I'm working on an assignment for an online course. It requires me to take an input formatted like Tony Stark 90.25 and sort it into one vector for the name and one for the score. My current code is
int numStudents;
int i;
char userSelection;
string newStudent;
double newGrade;
cin >> numStudents;
for(i = 0; i < numStudents; ++i){
cout << "Please enter student (First Last Grade) info:" << endl;
cin >> studentNames.at(i);
cin >> studentGrades.at(i);
}
I had no idea where to start with this so it could be way off the mark. Right now my name vector will store "Tony" and my score vector is storing 0.
The problem is that the cin ">>" operator parses text delimited by whitespace. So it reads in Tony, then sees the space, stops parsing. " Stark 90.25" will still be in the buffer, so it will attempt to parse "Stark" into your studentGrades element.
One option is to use getline, because getline is delimited by the newline instead of spaces. But this would be easier to use if the 90.25 part was on its own line.
If your format is always (First Last Grade), then perhaps it's easier just to use three variables.