LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 04-23-2008, 11:08 AM   #1
babag
Member
 
Registered: Aug 2003
Posts: 419

Rep: Reputation: 31
c++ - error: cannot convert ‘std::basic_string<char,


Code:
error: cannot convert ‘std::basic_string<char, std::char_traits<char>, std::allocator<char> >’ to ‘char’ in initialization
i get the above when i try to compile this:
Code:
#include <fstream>
#include <iostream>
#include <string>
#include <vector>

using namespace std;

int main()
{
   int count = 0;

   ifstream infile("/home/babag/Documents/Scripts/ScriptVariables.txt");

   vector<string> ScriptVariables;
   string line;

   while (getline(infile, line))
   {

      ScriptVariables.push_back(line);

   }

   infile.close();

   char headerA		= 	ScriptVariables[000];

   cout << ScriptVariables[000] << endl;
   cout << headerA << endl;

   return 0;
}
with help from those here i've gotten this to read each line of
a text file to an array, ScriptVariables[n].

i now want to reassign each of these array elements to another
variable with a friendlier name. that's what i'm trying to do
with:
Code:
char headerA = ScriptVariables[000];
but it gives me the error. this is just the first of a hundred,
or so, of these ScriptVariables[n] i'll reassign to new names.

also, some should be strings, some numerical. i assume there's
a way i can differentiate or convert them when i reassign the
new names?

thanks again for all the help,
BabaG
 
Old 04-23-2008, 01:02 PM   #2
ntubski
Senior Member
 
Registered: Nov 2005
Distribution: Debian, Arch
Posts: 3,780

Rep: Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081Reputation: 2081
ScriptVariables is a vector of strings, therefore ScriptVariables[0] is a string, and you can't assign a string to a char. Try
Code:
string headerA = ScriptVariables[0];
unless you just wanted to first letter, then you could do
Code:
 char headerA = ScriptVariables[0][0];
I strongly recommend you don't write numbers with leading zeros: in C(++) those are interpreted as octal numbers.

Quote:
also, some should be strings, some numerical. i assume there's
a way i can differentiate or convert them when i reassign the
new names?
You can use atoi or stringstreams

Code:
int headerA = atoi(ScriptVariables[0].c_str());

istringstream s;
s.str(ScriptVariables[0]);
s >> headerA;
If ScriptVariables[0] happens not to be a number, atoi will return 0, which makes error detection slightly more cumbersome. With stringstream, s.fail() returns true if the string doesn't parse.
 
Old 05-14-2008, 06:46 PM   #3
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
thanks ntubski. been a while. out of town.

got the atoi suggestion to work for most of what i need
but ran into a problem with a fractional variable. tried

[Code:]
double GamInc = atoi(ScriptVariables[75].c_str());
[/Code]

for a line in while the value is .5

in printing this variable (GamInc) i got 0 as the
returned value instead of .5

[Code:]
cout << GamInc << endl;
[/code]

what did i do wrong here?

thanks again,
BabaG

Last edited by babag; 05-14-2008 at 06:57 PM.
 
Old 05-14-2008, 07:06 PM   #4
tuxdev
Senior Member
 
Registered: Jul 2005
Distribution: Slackware
Posts: 2,012

Rep: Reputation: 115Reputation: 115
atoi only returns integers, so 0.5 gets truncated down to 0. Personally, I'd use stringstreams or sscanf, with a bias towards stringstreams.
 
Old 05-14-2008, 07:09 PM   #5
nadroj
Senior Member
 
Registered: Jan 2005
Location: Canada
Distribution: ubuntu
Posts: 2,539

Rep: Reputation: 60
atoi is for integers, so it only accepts +,-,0,1,...,9 (no other characters including decimals). maybe try atof
 
Old 05-15-2008, 11:50 AM   #6
babag
Member
 
Registered: Aug 2003
Posts: 419

Original Poster
Rep: Reputation: 31
tried atof since most of my input was strings and worked
with atoi. couldn't get atof to work, though.

double GamInc = atof(ScriptVariables[75]);

tried istringstreams briefly and didn't get it so moved
on to atof. will haave another look at istringstreams.
any tips on what i might have wrong with atof would be
much appreciated, though.

edit:
got atof working - found a better example online:

double GamInc = atof(ScriptVariables[75].c_str());

thanks,
BabaG

Last edited by babag; 05-15-2008 at 12:02 PM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
convert char * to int? calorie712 Programming 6 05-01-2006 04:38 AM
Convert Integer to Char gjagadish Programming 5 10-14-2005 10:09 AM
How to convert string to char? twirl Programming 27 10-13-2005 07:11 AM
C Problem---convert char to char* totti10 Programming 11 11-06-2004 11:32 AM
convert from char* to unsigned char* D J Linux - Software 2 02-20-2004 04:09 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:18 AM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration