Results 1 to 3 of 3
  1. #1
    New Lounger
    Join Date
    Oct 2007
    Posts
    1
    Thanks
    0
    Thanked 0 Times in 0 Posts
    I am in desperate need of help. I have been working on this program for 10 hours now. It compiles just fine, and will run, but it does not work correctly. The purpose of the program is to read the gender and the corresponding GPA from a data file. Then calculate the average GPA of both genders. When I run the program it comes back 0.0 for both averages which is incorrect. I do not know what I am doing wrong.

    The data file is formatted as so...
    m 1.0
    f 2.4
    m 3.3
    m 4.0
    etc...

    Code:
    //
    // Author: Lauren Settle
    //
    // Program: Average GPA by gender
    //
    // This program calculates the average male and average female GPA's.
    //
    
    #include <iostream>
    #include <iomanip>
    #include <fstream>
    #include <string>
    using namespace std;
    
    // function prototypes
    void openFiles (fstream& infile);
    void initialize (double& sumFemaleGPA, double& sumMaleGPA, double& averageMaleGPA, double& averageFemaleGPA, int& countMale, int& countFemale);
    void sumGrades (fstream& inFile, int& countMale, double& sumMaleGPA, int& countFemale, double& sumFemaleGPA);
    void averageGrades (double& sumMaleGPA, int& countMale, double& averageMaleGPA, double& sumFemaleGPA, int& countFemale, double& averageFemaleGPA);
    void printResults (fstream& inFile, double& averageMaleGPA, double& averageFemaleGPA);
    
    int main()
    
    {
    
        fstream inFile;
        int countFemale;
        int countMale;
        double sumMaleGPA;
        double sumFemaleGPA;
        double averageMaleGPA;
        double averageFemaleGPA;
        
        
        openFiles (inFile);
        initialize (sumFemaleGPA, sumMaleGPA, averageMaleGPA, averageFemaleGPA, countMale, countFemale);
        sumGrades (inFile, countMale, sumMaleGPA, countFemale, sumFemaleGPA);
        averageGrades (sumMaleGPA, countMale, averageMaleGPA, sumFemaleGPA, countFemale, averageFemaleGPA);
        printResults (inFile, averageMaleGPA, averageFemaleGPA);
        
        /* Scaffolding code for testing purposes */ 
    cin.ignore(256, '\n');
    cout << "Press ENTER to continue..." << endl;
    cin.get();
    /* End Scaffolding */ 
        return 0;
    }
    
    void openFiles (fstream& inFile)
    {
    
         inFile.open ("C:/user/documents/csc/GPAINFILE.TXT", ios::in);
         cout << fixed << showpoint;
         cout << setprecision(2);
         
    }
    
    void initialize (double& sumFemaleGPA, double& sumMaleGPA, double& averageMaleGPA, double& averageFemaleGPA, int& countMale, int& countFemale)
    {
         
         sumFemaleGPA = 0.0;
         sumMaleGPA = 0.0;
         averageFemaleGPA = 0.0;
         averageMaleGPA = 0.0;
         countMale = 0;
         countFemale = 0;
    
    }
    
    void sumGrades (fstream& inFile, int& countMale, double& sumMaleGPA, int& countFemale, double& sumFemaleGPA)
    {
         char gender;
         const char& m = 'm';
         const char& f = 'f';
         double GPA;
         GPA = 0.0;
         
         while (inFile.eof());
         {
               inFile >> gender >> GPA;
               if (gender == m)
                  { countMale++;
            sumMaleGPA = sumMaleGPA + GPA; }
            
         if (gender == f)
                      { countFemale++;
             sumFemaleGPA = sumFemaleGPA + GPA; }
             }
    
         
    }
    
    void averageGrades (double& sumMaleGPA, int& countMale, double& averageMaleGPA, double& sumFemaleGPA, int& countFemale, double& averageFemaleGPA)
    {
         if (countMale != 0)
         averageMaleGPA = (sumMaleGPA / countMale);
         if (countFemale != 0)
         averageFemaleGPA = (sumFemaleGPA / countFemale);
    }
    
    void printResults ( fstream& inFile, double& averageMaleGPA, double& averageFemaleGPA)
    {
         cout << " The average male GPA is " << averageMaleGPA << " and the average female GPA is " << averageFemaleGPA << "." << endl;
         inFile.close ();
    }

  2. #2
    Super Moderator RetiredGeek's Avatar
    Join Date
    Mar 2004
    Location
    Manning, South Carolina
    Posts
    9,436
    Thanks
    372
    Thanked 1,457 Times in 1,326 Posts
    Patti,

    Welcome to the lounge and I hope I got your name right from hour handle?

    Now I'm not a C++ guru but I used to be pretty good with C and I think your problem is that when you call your functions you are passing by value, i.e. passing the contents of a variable, when you want to be passing the address of that variable, passing by reference. When you pass by value the called routine, e.g. SumFemaleGPA has no way to pass the calculated value back to your Main procedure. If you pass by reference the called procedure uses the actual storage space of the variable declared in Main so the results are basically passed back to Main.

    Here is a tutorial onpointers in C++

    You have to be very careful in function based languages like C & C++ to be sure your functions can communicate by passing the correct type of information {values or addressed/references} to the called procedures. The thing to remember is that if you want the data changed by a function to be returned pass a reference {&variablename}. If the value being passed is only for the use of the function and you have no further use for it pass a value {variablename}

    From a quick refresher look through "The Annotated C++ Reference Manual" this is what I think you want:
    Code:
    //Function Call
    averageGrades (sumMaleGPA, countMale, &averageMaleGPA, sumFemaleGPA, countFemale, &averageFemaleGPA);
    
    //Function
    void averageGrades (double sumMaleGPA, int countMale, double *averageMaleGPA, double sumFemaleGPA, int& countFemale, double *averageFemaleGPA)
    {
    if (countMale != 0)
    averageMaleGPA = (sumMaleGPA / countMale);
    if (countFemale != 0)
    averageFemaleGPA = (sumFemaleGPA / countFemale);
    }
    I'm not quite sure why you are using all the & after the variable types? Again, I've been away from this for 15 years or so...so I may be completely out of date.

    Note: The different notation in how you call the function and how the function is declared.
    I hope I haven't confused you, it's been a long time, and this helps you solve your problems.
    May the Forces of good computing be with you!

    RG

    PowerShell & VBA Rule!

    My Systems: Desktop Specs
    Laptop Specs

  3. #3
    New Lounger
    Join Date
    Dec 2009
    Location
    Rock Hill, South Carolina
    Posts
    5
    Thanks
    0
    Thanked 0 Times in 0 Posts
    void sumGrades (fstream& inFile, int& countMale, double& sumMaleGPA, int& countFemale, double& sumFemaleGPA)
    {
    char gender;
    const char& m = 'm';
    const char& f = 'f';
    double GPA;
    GPA = 0.0;

    while (inFile.eof());
    {
    inFile >> gender >> GPA;
    if (gender == m)
    { countMale++;
    sumMaleGPA = sumMaleGPA + GPA; }

    if (gender == f)
    { countFemale++;
    sumFemaleGPA = sumFemaleGPA + GPA; }
    }
    in the sumGrades function you have a while loop. it needs to read
    while (!inFile.eof())
    instead of
    while (inFile.eof());
    inFile.eof() returns false unless it sees the endfile character so you need to use the not (!) character.In addition you don't put that semicolon there. i hope this helps!
    EDIT: i corrected a mistake i made.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •