1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86
|
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
void openFiles(ifstream&, ofstream&);
void initialize();
void sumGrades(ifstream, ofstream, int&, int&, double&, double&);
void averageGrade(int, int, double, double, double&, double&);
void printResults(ifstream, ofstream, int, int, double, double, double, double);
int main()
{
cout << "See final.txt for results." << endl;
initialize();
system("pause");
return 0;
}
void initialize()
{
int countFemale = 0;
int countMale = 0;
double sumFemaleGPA = 0.0;
double sumMaleGPA = 0.0;
double averageMaleGPA = 0.0;
double averageFemaleGPA = 0.0;
ifstream inGrades;
ofstream outGrades;
openFiles(inGrades, outGrades);
sumGrades(inGrades, outGrades, countFemale, countMale, sumFemaleGPA, sumMaleGPA);
averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageMaleGPA, averageFemaleGPA);
printResults(inGrades, outGrades, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageMaleGPA, averageFemaleGPA);
}
void openFiles(ifstream& inGrades, ofstream& outGrades)
{
inGrades.open("Ch7_Ex6Data.txt");
outGrades.open("final.txt");
outGrades << fixed << showpoint << setprecision(3);
}
void sumGrades(ifstream inGrades, ofstream outGrades, int& countFemale,
int& countMale, double& sumFemaleGPA, double& sumMaleGPA)
{
char gender;
double grade;
while (inGrades)
{
inGrades >> gender >> grade;
if (gender == 'm')
{
countMale++;
sumMaleGPA = sumMaleGPA + grade;
}
else
{
countFemale++;
sumFemaleGPA = sumFemaleGPA + grade;
}
}
}
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,
double& averageMaleGPA, double& averageFemaleGPA)
{
averageMaleGPA = sumMaleGPA / countMale;
averageFemaleGPA = sumFemaleGPA / countFemale;
}
void printResults(ifstream inGrades, ofstream outGrades, int countFemale, int countMale,
double sumFemaleGPA, double sumMaleGPA, double averageMaleGPA, double averageFemaleGPA)
{
outGrades << "Processing Grades." << endl;
outGrades << inGrades;
outGrades << "Sum Female GPA:" << sumFemaleGPA << endl;
outGrades << "Sum Male GPA:" << sumMaleGPA << endl;
outGrades << "Male count:" << countMale << endl;
outGrades << "Female count:" << countFemale << endl;
outGrades << "Male Average:" << averageMaleGPA << endl;
outGrades << "Female Average:" << averageFemaleGPA << endl;
inGrades.close();
outGrades.close();
}
|