while loop not reading eof

This is a discussion on while loop not reading eof within the C++ Programming forums, part of the General Programming Boards category; I'm working on this program and need help! For some reason the while loop is not recognizing the end of ...

  1. #1
    Registered User
    Join Date
    Jan 2006
    Posts
    1

    while loop not reading eof

    I'm working on this program and need help! For some reason the while loop is not recognizing the end of file int he text file.

    Here's my code:

    Code:
    transIn.open("trans1.txt");
          while (!transIn.eof())
         {
               withdrawalAmt = 0.0;
               depositAmt = 0.0;
               transAmt = 0.0;
               numTrans = 0;
                          transIn >> acctChar >> acctNum >> begBal;
                          ch = transIn.peek();
                          if (ch != '\n') {
                          do
               {
                                                                       transIn >> transChar;
                       if (transChar == 'W')
                       {
                         transIn >> transAmt;
                         withdrawalAmt = withdrawalAmt + transAmt;
                         numTrans++;
                       }
                       else if (transChar == 'D')
                       {
                         transIn >> transAmt;
                         depositAmt = depositAmt + transAmt;
                         numTrans++;
                       }
                                    ch = transIn.peek();
                               } while (ch != '\n');
               }
               cout << fixed << showpoint << setprecision(2);
               cout << acctChar << acctNum << " " << begBal << endl;
               cout << withdrawalAmt << " " << depositAmt << " " << numTrans << endl;
         }
    Any help would be appreciated!

  2. #2
    Registered User
    Join Date
    Jan 2006
    Location
    Washington D.C.
    Posts
    25
    You're probably never triggering it. Replace:

    Code:
    while (!transIn.eof())
    With

    Code:
    while (transIn.peek() != EOF)

  3. #3
    Just Lurking Dave_Sinkula's Avatar
    Join Date
    Oct 2002
    Posts
    5,006
    7. It is easier to write an incorrect program than understand a correct one.
    40. There are two ways to write error-free programs; only the third one works.*

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. help with reading from stream
    By movl0x1 in forum C Programming
    Replies: 7
    Last Post: 05-31-2007, 11:36 PM
  2. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  3. Replies: 3
    Last Post: 03-04-2005, 02:46 PM
  4. Reading an array in a while not end of file loop
    By npg316 in forum C Programming
    Replies: 5
    Last Post: 10-18-2004, 01:25 PM
  5. EOF not working in a loop
    By Malabux in forum C++ Programming
    Replies: 3
    Last Post: 10-12-2003, 07:28 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21