C++ reading data from file trouble

I am having trouble reading data in from a file that is formatted like so:

Name
State
Money per hour
Hours

It would look something like:

John Doe
California
10.50
45
Elaine Smith
Ohio
11.25
40

I have attempted to do:

1
2
3
4
5
6
7
8
9
10
11
while(!fin.eof())
      {
          while(getline(fin, name), getline(fin, state), fin >> money, fin >> hour)
          {
               p[used].set_name(name);
               p[used].set_state(state);
               p[used].set_money(money);
               p[used].set_hour(hour);
               used++
           }
      }


I'm getting stuck into an infinite loop.. I am unsure how to read in separate data per line. I know my method would work if the file were formatted like:

Name State Money Hour

How can I fix this?
Two while's are one too much, I would say. The extraction operator when evaluated as a bool triggers the stream's operator bool() overload, which in turn returns the value of the stream's good() method. So your code snippet seems to be looping OK in the inner loop but then the outer loop never completes because the end of file is never reached, meaning fin.eof() will never be true. Why? I am guessing because the last newline character is still there.

So try getting rid of the outer loop and see if that improves your situation.
Yes, that was the problem. Thank you very much. Can't believe it was such a simple issue such as that.
Topic archived. No new replies allowed.