title: C++ while loop to read from input file
body: <p>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 </p>
  
  <pre><code> while(InFile){code}
  </code></pre>
  
  <p>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. </p>
  
  <pre><code>void ProcessTransactions(Bank &amp;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 &gt;&gt; month &gt;&gt; day &gt;&gt; year;
          transDate.SetDate(month, day, year);
  
          InFile &gt;&gt; acctNum;
          InFile &gt;&gt; amount;
          InFile &gt;&gt; transType;
          if(transType == "Transfer")
              InFile &gt;&gt; transAcctNum;
  
          cout &lt;&lt; amount &lt;&lt; endl;
      }
  }
  </code></pre>
  
  <p>Input File</p>
  
  <pre><code>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
  </code></pre>
  
  <p>outputs</p>
  
  <pre><code>100
  100
  100
  200
  200
  50
  50
  80
  300
  100
  100
  100
  300
  300 //** Why is this output twice ?
  </code></pre>
  
  <p>After it extracts the last bit of data, the file marker should have reached EOF, thus terminating the loop. </p>
  
  <p>Any help would be much appreciated! </p>
  
  <p>=========================================================================
  Additional EXPLANATION / Solution :
  from: 
  <a href="https://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong">Why is iostream::eof inside a loop condition considered wrong?</a> </p>
  
  <p>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.</p>
  
  <p>Consider this (and assume then next read will be at the end of the stream)</p>
  
  <pre><code>while(!inStream.eof()){
    int data;
    // yay, not end of stream yet, now read ...
    inStream &gt;&gt; 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
   }
  </code></pre>
  
  <p>Against this:</p>
  
  <pre><code>int data;
  while(inStream &gt;&gt; data){
      // when we land here, we can be sure that the read was successful.
      // if it wasn't, the returned stream from operator&gt;&gt; would be 
      // converted to false
      // and the loop wouldn't even be entered
      // do stuff with correctly initialized data (hopefully)
  }
  </code></pre>