C++ prog help

This is a discussion on C++ prog help within the C++ Programming forums, part of the General Programming Boards category; Hey. I did a program that reads a file which contains names of students in a class. The names of ...

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

    C++ prog help

    Hey. I did a program that reads a file which contains names of students in a class. The names of the students are then used to read files which contain each students' grades. I then had to calculate the average of each individual student, then also calculate the average of the class and the max score and the min score of the class (not meaning the average, but each individual grade). My average isn't calculating right. It is off by a couple hundredths and I can't figure out why. Would anyone be willing to help? Thanks. Here's my code:

    Code:
    # include <iostream>
    # include <fstream>
    # include <cstdlib>
    # include <string>
    # include <iomanip>
    
    using namespace std;
    
    int main()
    {
             double w = -1;
    		 string fileName, firstName, lastName, middleInitial, studName;
    		 double countStud = 0, countSpaces = 0, x = 0;
    		 double count = 0, min = 101, max = 0;
    		 double classavg = 0;
    	     double sum = 0;
    
    		 cout << "Enter the name of the student file: ";
    		 cin  >> fileName;
    		 cout << endl;
    		 
             ifstream inData;
             inData.open(fileName.c_str());
             if (!inData)
                 cout << "File doesn't exist!" << endl;
             else
             {
    			 cout << "First\tMI  Last\t\tAverage\n" << endl;
                 
                 while (inData.good())
                 {
                       inData >> lastName
                              >> firstName
                              >> middleInitial;
                       studName = firstName + lastName + ".dat";       
                       count = 0; w = -1; sum = 0;
                       
                       ifstream inData2;
                       inData2.open(studName.c_str());
                       if (!inData2)
                          cout << firstName 
                               << "\t" 
                               << middleInitial
                               << "  " 
                               << lastName 
                               << "\t\tNO DATA FILE" 
                               << endl;
                       else
                           while (inData2.good())
                           {
                                 inData2 >> w;
    
            	                 if (w != -1)
            			         {
                                       count++;
    				                   if (min > w) min = w;
    				                   if (max < w) max = w;
                                 }
    			                 sum += w;
                           }
                        inData2.close();
                        inData2.clear();
                        
    		            if (count == 0) cout << firstName 
                                          << "\t" 
                                          << middleInitial 
                                          << "  " 
                                          << lastName
                                          << "\t" 
                                          << "NO GRADES" 
                                          << endl;
                      	else
    		            {
    			            sum = sum / count;
    					    classavg += sum;
    					    countStud++;
    					    cout << firstName 
                                 << "\t" 
                                 << middleInitial
                                 << "  " 
                                 << lastName 
                                 << "\t\t" 
                                 << fixed << setprecision(2) << sum 
                                 << endl;
                        }
                 }
                 
                 inData.close();
                 inData.clear();
                 
    			 classavg = classavg / countStud;
    		
    			 cout << endl;
    			 cout << endl;
                 cout << "Max Grade = " 
                      << fixed 
                      << setprecision(2) 
                      << max 
                      << endl;
    			 cout << "Min Grade = " 
                      << fixed 
                      << setprecision(2) 
                      << min 
                      << endl;
                 cout << endl;
                 cout << "Class Average = " 
                      << fixed 
                      << setprecision(2) 
                      << classavg;
             }         
             
    
           	 char d;
    		 cin >> d;
    		 return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2005
    Posts
    7,318
    >> while (inData.good())
    By doing this, you are likely using the last piece of data twice. The next line reads in a last name, but the file stream won't fail until your attempt to read in that last name fails. That means one extra iteration of the loop will run just re-using the data from the previous run.

    The solution is to use the return value of the read as the control for the loop (e.g. while (inData >> lastName)), or check the state of the stream after the first read attempt and then break the loop.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Assembly amature question: prog errors
    By geek@02 in forum Tech Board
    Replies: 1
    Last Post: 10-03-2008, 01:35 PM
  2. Getting input from another prog?
    By Badman3k in forum C Programming
    Replies: 4
    Last Post: 11-11-2004, 01:58 AM
  3. an option at the end of this prog to ask if I want to run again
    By bandito9111 in forum C++ Programming
    Replies: 2
    Last Post: 03-31-2003, 01:30 PM
  4. Try my prog...
    By Commander in forum A Brief History of Cprogramming.com
    Replies: 0
    Last Post: 05-09-2002, 07:43 AM
  5. password prog
    By ihsir in forum A Brief History of Cprogramming.com
    Replies: 10
    Last Post: 01-06-2002, 05:39 AM

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