2

I wrote a function that reads in transactions from an input file using a while loop. I can't for the life of me figure out why it is reading the last 2 lines twice though. When using

 while(InFile){code}

from my understanding it will continue looping until the file as reached the EOF marker. I can't figure out where I am going wrong here.

void ProcessTransactions(Bank &acctList, string fileName)
{

    Date transDate;
    ifstream InFile;
    InFile.open(fileName.c_str());
    int month;
    int day;
    int year;
    int acctNum;
    int transAcctNum;
    float amount;
    string transType;

    while(InFile)
    {
        InFile >> month >> day >> year;
        transDate.SetDate(month, day, year);

        InFile >> acctNum;
        InFile >> amount;
        InFile >> transType;
        if(transType == "Transfer")
            InFile >> transAcctNum;

        cout << amount << endl;
    }
}

Input File

5 1 2012    1212    100.00  Deposit
5 1 2012    2323    100.00  Deposit
5 1 2012    3434    100.00  Deposit
6 1 2012    1212    200.00  Withdrawal
6 1 2012    2323    200.00  Withdrawal
6 1 2012    3434    50.00   Withdrawal
7 1 2012    1212    50.00   Transfer
2323
7 1 2012    2323    80.00   Transfer
3434
7 1 2012    3434    300.00  Transfer
1212
9 1 2012    1212    100.00  Deposit
9 1 2012    2323    100.00  Deposit
9 1 2012    3434    100.00  Deposit
10 1 2012   1212    300.00  Transfer
1212

outputs

100
100
100
200
200
50
50
80
300
100
100
100
300
300 //** Why is this output twice ?

After it extracts the last bit of data, the file marker should have reached EOF, thus terminating the loop.

Any help would be much appreciated!

========================================================================= Additional EXPLANATION / Solution : from: Why is iostream::eof inside a loop condition considered wrong?

Because iostream::eof will only return true after reading the end of the stream. It does not indicate, that the next read will be the end of the stream.

Consider this (and assume then next read will be at the end of the stream)

while(!inStream.eof()){
  int data;
  // yay, not end of stream yet, now read ...
  inStream >> data;
  // oh crap, now we read the end and *only* now the eof bit will be 
  set (as well as the fail bit)
  // do stuff with (now uninitialized) data
 }

Against this:

int data;
while(inStream >> data){
    // when we land here, we can be sure that the read was successful.
    // if it wasn't, the returned stream from operator>> would be 
    // converted to false
    // and the loop wouldn't even be entered
    // do stuff with correctly initialized data (hopefully)
}
jslice
  • 89
  • 1
  • 1
  • 9
  • Possible duplicate of [How does reading file with while loops work in C++?](https://stackoverflow.com/questions/14009737/how-does-reading-file-with-while-loops-work-in-c) – jww Feb 18 '18 at 10:48

1 Answers1

9

After it extracts the last bit of data, it the file marker should have reached EOF, terminating the loop.

No.

EOF is set when you attempt to read past the end of the file. Here you do not check that your extraction succeeded, only that the stream is okay before you attempt extraction. Hence you'll get an extra iteration at the end.

You should loop like this (and there are plenty of examples of it on Stack Overflow, since we are constantly telling people how to do it):

while (InFile >> month >> day >> year)
Lightness Races in Orbit
  • 358,771
  • 68
  • 593
  • 989