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 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
|
/*********************************************************************
Name: Daniel Wilson
Project: Lab_6_exercise_6 (GPA Problem)
Date of Coding: 03/18/2011
Course: C++ IST-163-201
Instructor: Brian Morgan
Help Recieved: c++ textbook,in-class discussions
**********************************************************************/
#include<iostream>
#include<iomanip>
#include<fstream>
#include<string>
using namespace std;
//function prototypes
int openFiles();
int getSex(istream& in);
double getGPA(istream& in);
void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA);
void sumGrades(char sex,double gpa, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA);
void printResults(double averageFemaleGPA,double averageMaleGPA);
int main()
{
int countFemale;
int countMale;
double sumFemaleGPA;
double sumMaleGPA;
double averageFemaleGPA;
double averageMaleGPA;
char sex;
double gpa;
ifstream fin;
ofstream fout;
initialize(countFemale,countMale,sumFemaleGPA,sumMaleGPA);
openFiles();
sex = (getSex(fin));
gpa = getGPA(fin);
while (!fin.eof())
{
sumGrades(sex,gpa,countFemale,countMale,sumFemaleGPA,sumMaleGPA);
sex = getSex(fin);
gpa = getGPA(fin);
}
averageGrade(countFemale,countMale,sumFemaleGPA,sumMaleGPA,averageFemaleGPA,averageMaleGPA);
printResults(averageFemaleGPA,averageMaleGPA);
fin.close();
fout.close();
return 0;
}
int openFiles()
{
ifstream fin; //input stream variable for data
ofstream fout; //output stream variable
fin.open("inputdata.txt");
if (!fin)
{
cout << "Unable to open the input file." << endl;
cout << "This program terminates." << endl;
return 1;
}
fout.open("out.txt");
fout << fixed << showpoint;
fout << setprecision(2);
}
int getSex(istream& in)
{
char sex;
in >> sex;
return sex;
}
double getGPA(istream& in)
{
double gpa;
in >> gpa;
return gpa;
}
void initialize(int& countFemale,int& countMale,double& sumFemaleGPA,double& sumMaleGPA)
{
countFemale = 0;
countMale = 0;
sumFemaleGPA = 0.0;
sumMaleGPA = 0.0;
}
void sumGrades(char sex,double gpa, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA)
{
if(sex == 'f')
{
countFemale++;
sumFemaleGPA = sumFemaleGPA + gpa;
}
else
{
countMale++;
sumMaleGPA = sumMaleGPA + gpa;
}
}
void averageGrade(int countFemale, int countMale, double sumFemaleGPA, double sumMaleGPA,double& averageFemaleGPA,double& averageMaleGPA)
{
averageFemaleGPA = sumFemaleGPA / countFemale;
averageMaleGPA = sumMaleGPA / countMale;
}
void printResults(double averageFemaleGPA,double averageMaleGPA)
{
ofstream fout; //output stream variable
fout << endl << "The average female GPA is " << averageFemaleGPA << "." << endl;
fout << "The average male GPA is " << averageMaleGPA << "." << endl;
}
|