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
|
// GpaDraft2.cpp : Defines the entry point for the console application.
//
#include "stdafx.h"
#include <iostream>
#include <fstream>
#include <iomanip>
#include <string>
using namespace std;
void openFiles(ifstream&, ofstream&);
void initialize(ifstream&, ofstream&);
void sumGrades(double&, double&, ifstream&, ofstream&, int&, int&);
void averageGrade(int&, int&, double&, double&, ifstream&, ofstream&);
void printResults(int&, int&, double&, double&, ifstream&, ofstream&, double&, double&);
int _tmain(int argc, _TCHAR* argv[])
{
ifstream in;
ofstream out;
openFiles(in, out);
return 0;
}
void openFiles(ifstream& in, ofstream& out) {
in.open("student.txt");
out.open("avgGpa.txt");
out << fixed << showpoint;
out << setprecision(2);
initialize(in, out);
}
void initialize(ifstream& in, ofstream& out) {
char gender;
double GPA;
string line;
int countFemale = 0;
int countMale = 0;
double sumFemaleGPA = 0;
double sumMaleGPA = 0;
while (!in.eof()) {
in >> gender >> GPA;
if (gender == 'f') {
countFemale++;
sumFemaleGPA = sumFemaleGPA + GPA;
}
if (gender == 'm') {
countMale++;
sumMaleGPA = sumMaleGPA + GPA;
}
}
sumGrades(sumFemaleGPA, sumMaleGPA, in, out, countFemale, countMale);
averageGrade(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out);
}
void sumGrades(double& sumFemaleGPA, double& sumMaleGPA, ifstream& in, ofstream& out, int& countFemale, int& countMale) {
out << "Total Sum of GPA Grades: " << sumFemaleGPA + sumMaleGPA << endl;
}
void averageGrade(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, ifstream& in, ofstream& out) {
double averageFemaleGPA = sumFemaleGPA / countFemale;
double averageMaleGPA = sumMaleGPA / countMale;
printResults(countFemale, countMale, sumFemaleGPA, sumMaleGPA, in, out, averageFemaleGPA, averageMaleGPA);
}
void printResults(int& countFemale, int&countMale, double& sumFemaleGPA, double& sumMaleGPA, ifstream& in, ofstream& out, double& averageFemaleGPA, double& averageMaleGPA) {
out << "Total Females: " << countFemale << endl;
out << "Total Males: " << countMale << endl;
out << "Female Sum GPA: " << sumFemaleGPA << endl;
out << "Male Sum GPA: " << sumMaleGPA << endl;
out << "Female Average GPA: " << averageFemaleGPA << endl;
out << "Male Average GPA: " << averageMaleGPA;
in.close();
out.close();
}
|