Hello everybody,
I've run across a bug that I've been trying to correct. I've been given the sex and gpa of 14 different students in the form of a file. Every time I run my code, it states that it has read in one extra male. Any ideas? Thanks
Code:
For research purposes, the admission office of New College wants to know how well female and male students perform in certain courses. You received a file that contains female and male student GPAs for certain courses. Due to the confidentiality, the letter code f is used for female students and m for male students. Every entry consists of a letter code followed by a GPA. Each line has one entry. The number of entries in the file in unknown. Write a program that conputes and outputs the average GPA for female and male students. Format your results to two decimal places.
Create your own data file, use Notepad, name it GPAData.TXT.
Sample Data File.
f 4.0
m 4.0
f 3.5
m 3.5
f 3.0
m 3.0
f 2.5
m 2.5
f 2.0
m 2.0
f 1.0
m 1.0
f 0.0
m 0.0
Date: 7/20/08
*/
#include <fstream>
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
ifstream inFile;
char sex;
char m,f;
double gpa;
double fsum, favg;
double msum, mavg;
int fCount;
int mCount;
int counter;
fCount = 0;
mCount = 0;
counter = 0;
fsum = 0;
msum = 0;
favg = 0;
mavg = 0;
inFile.open("C:\\Users\\nathan\\Desktop\\C\\MY Work\\CPP5_17\\GPAData.txt");
if(!inFile)
{
cerr << "\nError opening CH5_Ex17Data.txt file." << endl;
}
cout << fixed << showpoint << setprecision(2);
cout << "\nReading the text file.\n" << endl;
while(inFile)
{
inFile >> sex >> gpa;
counter++;
switch(sex)
{
case 'f':
fCount++;
fsum = fsum + gpa;
favg = fsum/fCount;
break;
case 'm':
mCount++;
msum = msum + gpa;
mavg = msum/mCount;
break;
}
}
cout << "F Count: " << fCount << endl;
cout << "M Count: " << mCount << endl;
cout << "Count++: " << counter << endl;
cout << "F GPA: " << fsum << endl;
cout << "M GPA: " << msum << endl;
cout << "Favg: " << favg << endl;
cout << "Mavg: " << mavg << endl;
inFile.close();
cout << endl;
system("pause");
return 0;
}