mardi 22 janvier 2019

String variable wont take new value from getline() in a loop

I'm parsing data in from a file, and filling an array of structs with a string, float, and int member variable. I use if statements on the first character of my getline() string to test its type, then either declare a string as the getline value or use stof or stoi to convert the getline() value into a float/int. The program passes new values to the float and int loops, but repeatedly passes the first string encountered to my constructor function. Any help with this issue would be greatly appreciated.

int length=0;
string _username;
float _gpa;
int _age;
string line;
while(into.eof()==false)
{
    getline(into, line, ',');
    if(line.at(0)>'9')
    {
        _username=line;
    }
    if(line.at(1)=='.')
    {
      _gpa=stof(line);
    }
    if(line.at(0)<='9' && line.at(1)!='.' && line.at(0)>='0')
    {
      _age=stoi(line);
      users[1000].addUser(users, _username, _gpa, _age, length);
      length++;
    }
}

If the usernames are supposed to read: Mark, Chuck, Sarah, this code will instead construct the usernames as: Mark, Mark, Mark

Aucun commentaire:

Enregistrer un commentaire