Need Help Please!!!

I have to create a program that reads data from a file and calculates the GPA average for female and male students.

The program must contain these functions, openResults, initialize, sumGrades, averageGrades, and printResults.

The problem I"m having is that when I run the program when finding the female's GPA all it does is output the added GPA of the male students.

This is the data that I'm using
M 3.45
M 2.56
F 1.98
M 4.00
F 3.00
M 3.89
F 2.99
M 3.76
M 2.89
F 1.88
M 2.67

Here is what I have so far...

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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

void openFiles(ifstream& inFile, ofstream& outFile);
void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);
void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);
void averageGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& averageFemaleGPA, double& sumMaleGPA, double& averageMaleGPA);
void printResults(ofstream& outFile, ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);

int main()
{
	ifstream inFile;
	ofstream outFile;

	double averageFemaleGPA;
	double averageMaleGPA;

	double sumFemaleGPA;
	double sumMaleGPA;

	int countFemale;
	int countMale;

	openFiles(inFile, outFile);
	printResults(outFile, inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);

	return 0;
}

void openFiles(ifstream& inFile, ofstream& outFile)
{
	outFile << fixed << showpoint << setprecision(2);

	inFile.open("StudentsGPA.txt");
	outFile.open("AverageStudentGPA.txt");
}

void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA,
	double& averageFemaleGPA, double& averageMaleGPA)
{
	countFemale = 0;
	countMale = 0;
	sumFemaleGPA = 0;
	sumMaleGPA = 0;
	averageFemaleGPA = 0;
	averageMaleGPA = 0;
}

void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA,
	double& averageFemaleGPA, double& averageMaleGPA)
{
	
	initialize(countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);

	char Gender;

	double GPA;


	while (inFile >> Gender >> GPA)
	{
		switch (Gender)
		{
			case 'F':
				sumFemaleGPA = sumFemaleGPA + GPA;
				
				countFemale++;
				break;

			case 'M':
				sumMaleGPA = sumMaleGPA + GPA;
		
				countMale++;
				break;
		}
		
	}
	
	
}

void averageGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& averageFemaleGPA, double& sumMaleGPA, double& averageMaleGPA)
{
	sumGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);

	averageFemaleGPA = sumFemaleGPA / countFemale;


	averageMaleGPA = sumMaleGPA / countMale;
	
	
}

void printResults(ofstream& outFile, ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA)
{
	averageGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);

	outFile << "F  " << averageFemaleGPA << endl;
	outFile << "M  " << averageMaleGPA << endl;
	outFile << countFemale << endl;
	outFile << countMale << endl;
}


Any help would be most appreciated and Thank You.
Last edited on
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
#include<iostream>
#include<fstream>
#include<string>
#include<iomanip>

using namespace std;

void openFiles(ifstream& inFile, ofstream& outFile);
void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);
void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA);
void averageGrades(ifstream& inFile, int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA);
void printResults(ofstream& outFile, int &countFemale, int &countMale, double &averageFemaleGPA, double &averageMaleGPA);

int main()
{
	ifstream inFile;
	ofstream outFile;

	double averageFemaleGPA;
	double averageMaleGPA;

	double sumFemaleGPA;
	double sumMaleGPA;

	int countFemale;
	int countMale;

	openFiles(inFile, outFile);
	initialize(countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
	sumGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA);
	averageGrades(inFile, countFemale, countMale, sumFemaleGPA, sumMaleGPA, averageFemaleGPA, averageMaleGPA);
	printResults(outFile, countFemale, countMale, averageFemaleGPA, averageMaleGPA);

	return 0;
}

void openFiles(ifstream& inFile, ofstream& outFile)
{
	outFile << fixed << showpoint << setprecision(2);

	inFile.open("StudentsGPA.txt");
	outFile.open("AverageStudentGPA.txt");
}

void initialize(int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA,
	double& averageFemaleGPA, double& averageMaleGPA)
{
	countFemale = 0;
	countMale = 0;
	sumFemaleGPA = 0;
	sumMaleGPA = 0;
	averageFemaleGPA = 0;
	averageMaleGPA = 0;
}

void sumGrades(ifstream& inFile, int& countFemale, int& countMale, double& sumFemaleGPA, double& sumMaleGPA)
{
	
	

	char Gender;

	double GPA;


	while (inFile >> Gender >> GPA)
	{
		switch (Gender)
		{
			case 'F':
				sumFemaleGPA = sumFemaleGPA + GPA;
				
				countFemale++;
				break;

			case 'M':
				sumMaleGPA = sumMaleGPA + GPA;
		
				countMale++;
				break;
		}
		
	}
	
	
}
void averageGrades(ifstream& inFile, int &countFemale, int &countMale, double &sumFemaleGPA, double &sumMaleGPA, double& averageFemaleGPA, double& averageMaleGPA)
{
	averageFemaleGPA = sumFemaleGPA / countFemale;
	averageMaleGPA = sumMaleGPA / countMale;
}


void printResults(ofstream& outFile, int &countFemale, int &countMale, double &averageFemaleGPA, double &averageMaleGPA)
{
	

	outFile << "F  " << averageFemaleGPA << endl;
	outFile << "M  " << averageMaleGPA << endl;
	outFile << countFemale << endl;
	outFile << countMale << endl;
}
Topic archived. No new replies allowed.