0
int i = 0;
while(!fin.eof())
{

    fin >> fname;
    fin >> lname;
    fin >> id;

    customer[i].setFname(fname);
    customer[i].setLname(lname);
    customer[i].setId(id);

    i++;
}
fin.close();

When my program gets to the fourth iteration of this while loop (and runs out of text from my input .txt file) visual studio prompts me with this:

Unhandled exception at 0x754bc41f in program5.exe: Microsoft C++ exception: std::ios_base::failure at memory location 0x003ceb64..

I'm clueless as to why. I've tried replacing !fin.eof() with (fin >> x) where x is an int, I've tried a do-while instead of a while, I've tried using get, getline. I thought it might be an issue that eof wasn't triggering with my >> operator so I added fin.peek(); at the very end of the loop so it would trigger eof when it peeked. I just have no idea what I'm doing wrong. Assume my customer class works properly, I've tested outside of this loop at it works perfectly.

1 Answers1

1

If I recall, one of the idioms when working with streams is to make loops like

while (cin >> x){
   //do something...

You should be checking the state of the stream while you read in the values. You could do something like

while(fin >> fname && fin >> lname && fin >> id){
...

This could probably be made more elegant

Viknesh
  • 505
  • 1
  • 4
  • 14