Parsing strings

Hi,

There's a file with students' first name, last name and grades, i.e.:

1
2
3
4
[grades.txt]
johnny ray 79 43 23 32 99
lone ranger 54 88 78 94 34
alpha beta 76 83 91 100 32


What algorithm would you use to make the parsing simple?

I got stuck at the inside of the while loop, as I'm not sure how to parse each line 'neatly':

1
2
3
4
        while (getline(infile, line))
	{
					
	}
You could simply use operator >> instead of getline :)


1
2
3
in >> first >> last;
for( int i = 0; i < 5; ++i )
in >> grades[i];
Last edited on
Hi gilbit,

Thank you.

in your example, in is a file object, while first and last are string objects?

the >> operator overload:
1- inserts to 'first' the character until white-space is hit?
2- returns the file with its mark pointing to the next character after white-space?
Last edited on
You are correct the operator >> reads until it hits a whitespace , and yeah in was used as an object of type ifstream and the first/last were variable names used to represent strings where you are going to store the names.


Topic archived. No new replies allowed.