Hey again! I ran into a new problem in my university course exercises. Basically, I'm trying to read things from a .txt file. Every line is supposed to get converted into an object called "Client", and lines are formatted like this:
<account:int> <firstName:string> <lastName:string> <balance:double>
I have written my own string tokenizer to get individual strings from the whole line string. Everything else works fine, but I can't get the balance (double) to work.
Here's my tokenizer code (the "char tokenizer" isn't in use yet :) ):
string getToken(string line, char tokenizer, int pass) {
string token = "";
line += " ";
if(!line.empty()) {
if(pass == 0) {
for(int i = 0; i < line.find_first_of(" "); i++) {
if(line.at(i) != ' ' && !line.empty())
token += line.at(i);
}
}else if(pass > 0) {
for(int i = 0; i < pass; i++)
line.erase(0, (line.find_first_of(" ") + 1));
for(int i = 0; i <= line.find_first_of(" "); i++) {
if(line.at(i) != ' ')
token += line.at(i);
}
}
}
if(token.at(token.length()-1) == ' ')
token.erase(token.length()-1);
return token;
}
And here's the actual code which tries to put the strings into variables:
if(!line.empty()) {
istringstream ss;
ss.str(getToken(line, ' ', 0));
ss >> account;
firstName = getToken(line, ' ', 1);
lastName = getToken(line, ' ', 2);
ss.str(getToken(line, ' ', 3));
ss >> balance;
cout << account << "\n" << firstName << "\n" << lastName << "\n" << balance << "\n" << endl;
vektori.push_back(Client(account, firstName, lastName, balance));
}
So, the last "cout" produces nice information for the first three entries, but messes up the last one:
1234
John
Smith
3.35323e-307 (should be 80)