Thread: Reading a File/Counter Trouble

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    13

    Reading a File/Counter Trouble

    Hello everybody,
    I've run across a bug that I've been trying to correct. I've been given the sex and gpa of 14 different students in the form of a file. Every time I run my code, it states that it has read in one extra male. Any ideas? Thanks
    Code:
    For research purposes, the admission office of New College wants to know how well female and male students perform in certain courses.  You received a file that contains female and male student GPAs for certain courses.  Due to the confidentiality, the letter code f is used for female students and m for male students.  Every entry consists of a letter code followed by a GPA.  Each line has one entry.  The number of entries in the file in unknown.  Write a program that conputes and outputs the average GPA for female and male students.  Format your results to two decimal places.  
    Create your own data file, use Notepad, name it GPAData.TXT.
    Sample Data File.
    f 4.0
    m 4.0
    f 3.5
    m 3.5
    f 3.0
    m 3.0
    f 2.5
    m 2.5
    f 2.0
    m 2.0
    f 1.0
    m 1.0
    f 0.0
    m 0.0
       Date: 7/20/08
    */
    
    
    #include <fstream>
    #include <iostream>
    #include <iomanip>
    
    using namespace std;
    
    int main()
    {
       ifstream inFile;
    
       
       
       char sex;
       char m,f;
       double gpa;
       double fsum, favg;
       double msum, mavg;
       int fCount;
       int mCount;
       int counter;
       fCount = 0;
       mCount = 0;
       counter = 0;
       fsum = 0;
       msum = 0;
       favg = 0;
       mavg = 0;
       
    inFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP5_17\\GPAData.txt");
       if(!inFile)
       { 
         cerr << "\nError opening CH5_Ex17Data.txt file." << endl;
       }
    
    
    cout << fixed << showpoint << setprecision(2);
    
    cout << "\nReading the text file.\n" << endl;
    
    while(inFile)
    {
           inFile >> sex >> gpa;
         counter++;
              switch(sex)
              {
                         case 'f':
                              fCount++;
                              fsum = fsum + gpa;
                              favg = fsum/fCount;
                              break;
                         
                         case 'm':
                              mCount++;
                              msum = msum + gpa;
                              mavg = msum/mCount;
                              break;
               }
    }
                              
    cout << "F Count: " << fCount << endl;
    cout << "M Count: " << mCount << endl;
    cout << "Count++: " << counter << endl;
    cout << "F GPA: " << fsum << endl;
    cout << "M GPA: " << msum << endl;
    cout << "Favg: " << favg << endl;
    cout << "Mavg: " << mavg << endl;  
      
       inFile.close();  
    
       cout << endl;
       system("pause");
       return 0;
    }

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    After reading the last student, inFile is still in a "good" state, so the while loop goes around one more time; but of course, no new sex and gpa are read in, hence the last student in the file gets processed twice. You must check for a bad state in your input file immediately after trying to read, not immediately before.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    tab - I don't quite follow - how do I stop the code from reading itself again? Thx

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    The if (infile) checks whether infile is in a "good" state -- it will only be in a bad state AFTER you try to read past the end of the file. So you must do the check AFTER trying to read, not before.

  5. #5
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    I believe I corrected the code as you said, but the problem still persists.

    Code:
    inFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP5_17\\GPAData.txt");
    
    cout << "\nReading the text file.\n" << endl;
      
       if(!inFile)
       { 
         cerr << "\nError opening CH5_Ex17Data.txt file." << endl;
       }
    cout << fixed << showpoint << setprecision(2);

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    I've also tried
    Code:
     while(!inFile.eof())
    without success.

  7. #7
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    AFTER is the key here.
    Code:
    inFile >> sex >> gpa;
    while (inFile) {
        // do stuff
        inFile >> sex >> gpa;
        //now go back to the top, where the check of inFile is
    }
    You need to check that inFile is good AFTER you try to read something in.

  8. #8
    Registered User
    Join Date
    Jun 2008
    Posts
    13
    BEAUTIFUL!!! Thank you for your patience and time Tabstop!

  9. #9
    The larch
    Join Date
    May 2006
    Posts
    3,573
    Or without code reduplication:

    Code:
    while (inFile >> sex >> gpa)
    {
         do_stuff
    }
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems in reading binary file
    By serena in forum C Programming
    Replies: 3
    Last Post: 04-14-2005, 03:54 AM
  2. trouble reading data from a file
    By bob56 in forum C Programming
    Replies: 14
    Last Post: 03-17-2005, 09:03 AM
  3. reading from a text file help......
    By jodders in forum C++ Programming
    Replies: 2
    Last Post: 01-25-2005, 12:51 PM
  4. Reading a record from a File
    By David101 in forum C Programming
    Replies: 2
    Last Post: 12-14-2004, 06:42 PM
  5. trouble reading from a file...
    By Nutka in forum C Programming
    Replies: 7
    Last Post: 12-02-2002, 10:59 PM