darko
darko

Reputation: 2458

check my logic? (average gpa calculating program)

program reads the following data from a .txt file:

f   3.40
f   4.00
m   3.56
m   3.80
f   2.30
f   3.95
m   3.90
m   4.00
m   2.00
f   4.00
f   2.80
m   3.70
m   2.98
f   3.89
m   4.00
f   3.90
m   1.90
m   2.90
f   1.50
f   2.67
m   3.80
m   2.35

Then calculates the average (gpa) for each gender (m for male, f for female)

according to my calculations there are:

14 fs, the sum of gpas for f is 46.99, so the average gpa for f is 46.99/14 = 3.36.

and

14 ms, the sum of gpas for m is 46.64, so the average gpa for m is 46.64/14 = 3.33.

but what the program outputs in a new file is: average male GPA: 3.42 average female GPA: 3.76

Am i missing something in my operations?

my code:

header:

#ifndef header_h
#define header_h
#include <iostream>
#include <iomanip>
#include <fstream>
#include <cstring>
#include <cstdlib>
using namespace std;
void extern initialize(int&, int&, float&, float&, float&, float&);
void extern openFiles(ifstream&, ofstream&);
void extern sumGrades(istream&, ostream&, int&, int&, float&, float&);
void extern averageGrade (float&, float&, float, int, float, int);
void extern printResults (float, float, ostream&);
#endif

main:

#include "header.h"
int main()
{
    char gender;
    float gpa, sumFemaleGPA, sumMaleGPA, maleGrade, femaleGrade;
    ifstream inData;
    ofstream outData;
    int countFemale, countMale;

    openFiles(inData, outData);
    initialize (countFemale, countMale, sumFemaleGPA, sumMaleGPA, maleGrade, femaleGrade);
    sumGrades(inData, outData, countFemale, countMale, sumFemaleGPA, sumMaleGPA);
    averageGrade (maleGrade, femaleGrade, sumMaleGPA, countMale, sumFemaleGPA, countFemale);
    printResults(maleGrade, femaleGrade, outData); 


    system("PAUSE");
    return EXIT_SUCCESS;
}

openFiles:

    #include "header.h"

    void openFiles(ifstream& inData, ofstream& outData)
    {

    inData.open("./Ch7_Ex4Data.txt");
    outData.open("./Ch7_Ex4Dataout.txt");

    outData << fixed << showpoint << setprecision(2);        
} 

initialize:

#include "header.h"

void initialize (int& countFemale, int& countMale, float& sumFemaleGPA, float& sumMaleGPA, float& maleGrade, float& femaleGrade)
{
            countFemale = 0;
            countMale = 0;
            sumFemaleGPA = 0;
            sumMaleGPA = 0;
            maleGrade = 0;
            femaleGrade = 0;
}

sumGrades:

#include "header.h"

void sumGrades(istream& inData, ostream& outData, int& countFemale, int& countMale, float& sumFemaleGPA, 
               float& sumMaleGPA)
{

     char gender;
     float gpa;

   while( inData >> gender >> gpa )
    {

     inData >> gender >> gpa;

           if(gender == 'm')
              {
                         sumMaleGPA += gpa;
                         countMale++;   
              }         

     else if (gender == 'f')
              {
                      sumFemaleGPA += gpa;
                      countFemale++;
              }
    }

}                  

averageGrade:

#include "header.h"

void averageGrade (float& maleGrade, float& femaleGrade, float sumMaleGPA, int countMale, float sumFemaleGPA, int countFemale)
{
     maleGrade = sumMaleGPA / countMale;

     femaleGrade = sumFemaleGPA / countFemale;
}     

printResults:

#include "header.h"

void printResults(float maleGrade, float femaleGrade, ostream& outData) 
{
     outData << "average male GPA: " << maleGrade << endl;
     outData << "average female GPA: " << femaleGrade << endl;    
}     

Upvotes: 2

Views: 1689

Answers (1)

Paul Wheeler
Paul Wheeler

Reputation: 20180

Wouldn't these lines: skip every other line:

while( inData >> gender >> gpa )
{
  inData >> gender >> gpa;

You're using the return of the >> operator to indicate success for the test of the while loop, but each time it executes that, does it not consume that line and move to the next, so then when you execute it again inside the while loop you effectively overwrite the data from line 1 with the data from line 2 and ignore line 1.

Upvotes: 3

Related Questions