Thread: .eof will not work in iostream

  1. #1
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    .eof will not work in iostream

    My program works the only problem I am having is that when it reads the end of file it adds the last score to the total again. here is my program what have I done wrong with.eof?

    #include <iostream>
    #include <fstream> // File Stream Library

    using namespace std;

    int main()

    {
    char team;
    int numb, k=0, n=0;
    ifstream gamefile;// ifstream variable name declaration
    // open file
    gamefile.open("/home1/c/a/acsi201/data/game");

    //if the file fails to open it will return this statement
    if(gamefile.fail())
    {
    cout << "Input file opening failed.\n";
    exit(1);
    }

    /*while loop keeps reading file till the end of file with if else
    statements to compile the scores the winning team would be displayed
    first.*/

    while(!gamefile.eof()) // while loop
    {
    gamefile >> team >> numb; //reading the info provided by the file

    /* begining of if statement the character K stand for the Knicks
    and if team equals K than it will return knicks points scored
    otherwise it will return the Nets score.*/

    if(team == 'K')
    k = k + numb;
    else
    n = n + numb;
    if(k > n)

    /* Another if statement for winning team to go first.If the Knicks
    score is greater than the Nets than the Knicks score will be
    read first otherwise the Nets score will be read first.*/

    cout << "Knicks " << k << " Nets " << n << endl;

    else
    cout << "Nets " << n << " Knicks " << k << endl;
    }
    // end of whileloop

    gamefile.close();// closing of file previousely opened
    cout << "Good Game! \n";

    /* end of program since a output file was not required, to see output
    after compilation run file hw5 < result than at prompt type more
    result*/
    }

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    any ideas?

  3. #3
    So the loop goes one iteration farther than you want it to???
    My Avatar says: "Stay in School"

    Rocco is the Boy!
    "SHUT YOUR LIPS..."

  4. #4
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    This is a common problem, it happens because you are using gamefile.eof() incorrectly. The streamstate is set to eof only after you've attempted and failed a read, so the final iteration is really one further than you want. Instead, try either this:
    Code:
    while ( gamefile.good() ) {
      // read data
    .
    .
    or
    Code:
    while ( gamefile >> team >> numb ) {
    .
    .
    -Prelude
    Last edited by Prelude; 10-25-2002 at 12:11 AM.
    My best code is written with the delete key.

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    14
    Yes

  6. #6
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    Unhappy

    I guess I will just turn it in. It looked fine until I noticed the output added an the last score again and I have spent 5 hours playing with it to get it to come out right
    Jessica

  7. #7
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I guess I will just turn it in.
    I've answered your question and given you two solutions to the problem, did you miss my post?

    -Prelude
    My best code is written with the delete key.

  8. #8
    Registered User
    Join Date
    Apr 2002
    Posts
    14

    turnin

    no I did not miss them, I could not view the info from the file after compiling it with either solution. maybe it works but I cant' see the results like I could before nothing appears but thank you for trying.
    Jessica

  9. #9
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >I could not view the info from the file after compiling it with either solution
    Then you didn't implement either correctly. Perhaps if you posted your code and a piece of the file that you're reading I can help you fix the problem better. My test works quite nicely:
    Code:
    #include <iostream>
    #include <fstream>
    
    using namespace std;
    
    int main()
    {
      char team;
      int numb, k=0, n=0;
      ifstream gamefile;
      
      gamefile.open("test.txt");
      
      if(gamefile.fail())
      {
        cout << "Input file opening failed.\n";
        exit(1);
      }
      
      while(gamefile.good())
      {
        gamefile>> team >> numb;
        
        if(team == 'K')
          k = k + numb;
        else
          n = n + numb;
    
        if(k > n)
          cout << "Knicks " << k << " Nets " << n << endl;
        else
          cout << "Nets " << n << " Knicks " << k << endl;
      }
      
      gamefile.close();
      
      cout << "Good Game! \n";
      
      return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM