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
|
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
int main ()
{
double mCount=0, fCount=0, ccCount=0, unCount=0, score;
string inputPathName, name, line, collegeType;
double avgScore, mAverage, fAverage, ccAverage, unAverage, totalAverage;
double fTotalScore = 0.0, mTotalScore = 0.0, ccTotalScore = 0.0, unTotalScore = 0, totalScore = 0.0;
ifstream dataIn;
char gender = '\0';
cout << fixed << showpoint << setprecision(2);
//Prompt user to input file name. Open file. If an invalid file re-prompt user.
do {
cout << "Please enter input path name: ";
cin >> inputPathName;
dataIn.open(inputPathName);
if (!dataIn.is_open()) {
cout << "File input failed" << endl;
}
} while (!dataIn.is_open());
//Echo file
while (dataIn.peek() != EOF)
{
getline(dataIn, line);
cout << line << endl;
}
cout << endl << "End of file reached." << endl;
//Clear and rewind.
dataIn.clear();
dataIn.seekg(0);
//Read the information and determine averages for: males, females, community college students, university students,
//and the total average score for the survey.
while (!dataIn.eof()) {
dataIn >> name >> gender >> collegeType >> score;
if(gender =='M')
{
mCount++;
mTotalScore = mTotalScore + score;
}
else
{
fCount++;
fTotalScore = fTotalScore + score;
}
if(collegeType == "CC")
{
ccCount++;
ccTotalScore = ccTotalScore + score;
}
else
{
unCount++;
unTotalScore = unTotalScore + score;
}
mAverage = (mTotalScore / mCount);
fAverage = (fTotalScore / fCount);
ccAverage = (ccTotalScore / ccCount);
unAverage = (unTotalScore / unCount);
totalAverage = (fTotalScore + mTotalScore)/(mCount + fCount);
}
//cout<<"# of Men = "<<mCount<<endl;
//cout<<"Men total = " << mTotalScore<<endl;
cout<<"Men average = "<< mAverage<<endl;
//cout<<"# of Female = "<<fCount<<endl;
cout<<"Female total = " << fTotalScore<<endl;
cout<<"Female average = "<< fAverage<<endl;
cout<<fCount<<endl;
//cout << "# of CC students = " <<ccCount << endl;
//cout << "# of UN students = " <<unCount << endl;
cout<<"CC average = "<< ccAverage<<endl;
cout<<"UNtotal = " << unTotalScore<<endl;
cout<<"UN average = "<< unAverage<<endl;
cout<<unCount<<endl;
cout<<"Total average = "<< totalAverage<<endl;
char reply;
cout << "press q and enter to quit: ";
cin >> reply;
dataIn.close();
system("pause");
return 0;
}
|