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) == ' ')
    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:

3.35323e-307 (should be 80)

Edited by bleedi: n/a

6 Years
Discussion Span
Last Post by firstPerson

Did you check the token string you get that should contain the balance? I would suspect it turns out to be "0" instead of "80" or errorneous. 3.35323e-307 is a really weird number, but it is a number very close to 0, so it might actually read 0 and not 80. This might help understand the problem.

BTW: you could use substr() function instead of loops to extract the characters of the returned token one by one, it is less error-prone to use built-in functions.


Actually, the token string has the right balance, the data changes that way when it's converted to double.

I used the per-character extraction loop just for the exercise's purposes. :)


Actually, I feel quite stupid working with this for over three hours yesterday. I found the solution:

while ( file >> account >> firstName >> lastName >> balance )
    Client the_Client(account, firstName, lastName, balance);

This extracts the right stuff, to the right variable, in the right form. So, basically I wrote all the code I posted earlier for nothing... Well, next time I'll surely remember :P

Edited by bleedi: n/a


a better solution would be to define an operator>>. For example :

std::istream& operator>>(istream& is, Client& c){
 return is >> c.acccount >> firstName >> lastName >> balance;

//...in main somewhere
Client c;
while( file >> c) vectorVariable.push_back(c);
This question has already been answered. Start a new discussion instead.
Take the time to help us to help you. Please be thoughtful and detailed and be sure to adhere to our posting rules.