PDA

View Full Version : problem converting string-char-int



mickey
10th December 2006, 00:38
Hi,
I'm trying to read from a file a char for time (that are float eg line of file: 1.1 1.0 3);


vector <vector <float> > example;
char ch;
string s;
int i=0,j=0;
while (f.readChar(ch)) { //this is my function return a char on ch
cout.put(ch);
if (ch == '\n') lines++;
else if (ch != ' ') {
s += ch;
} else {
example[i][j] = atof(s.c_str()); //but here a runtime error uccurs
s="";
j++;
}

i++;
}

How to do that conversion? And then: is this the easier way to reach my aim? (I suspect it can be more simple).
thanks

wysota
10th December 2006, 00:43
vector <vector <float> > example;
//...
example[i][j] = atof(s.c_str()); //but here a runtime error uccurs

Did you initialise those vectors?

Try this:

vector<vector<float> > example(10, vector<float>(10, 0.0));
example[i][j] = atof(s.c_str());


And then: is this the easier way to reach my aim? (I suspect it can be more simple).
Yes. Use Qt :)