Problem with reading in values for calculations

Hi everyone! I'm working on a code to read information from a text file, calculate averages, and names. My infile looks like this:

Johnson 85 83 77 91 76
Aniston 80 90 95 93 48
Cooper 78 81 11 90 73
Gupta 92 82 30 69 87
Blair 23 45 96 38 59
Clark 60 85 45 39 67
Kennedy 77 31 52 74 83
Bronson 93 94 89 77 97
Sunny 79 85 28 93 82
Smith 85 72 49 75 63

And my code looks like:

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

using namespace std;

void CalculateAverage(ifstream& inp, ofstream& outp, double& courseAvg);

char CalculateGrade(double courseAvg);

int main()
{
	int counter;
	string name;
	double classAvg = 0;
	char grade;
	double courseAvg;
	
	ifstream infile;
	ofstream outfile;

	cout << "The output file has been created successfully and is located \non the root directory,";
	cout <<	" filename classGrades.txt.\n\n";

	infile.open ("c:\\classStudents.txt");
	outfile.open ("c:\\classGrades.txt");

	outfile << fixed << showpoint << setprecision(1);
	outfile << "Student  Test1 Test2 Test3 Test4 Test5 Average Grade" <<endl;

	for (counter = 1; counter <= 10; counter++)
{
	infile >> name;
	outfile << left << setw(11) << name;

	CalculateAverage(infile, outfile, courseAvg);

	grade = CalculateGrade(courseAvg);
	
	outfile << setw(8) << courseAvg << setw(6) << grade <<endl;
	
	}

	classAvg = classAvg + courseAvg;
	classAvg = classAvg / 10;

	outfile << "\nClass Average = " << classAvg <<endl;
	
	infile.close();
	outfile.close();

return 0;

}

void CalculateAverage(ifstream& inp, ofstream& outp, double& courseAvg)
{
	int score;
	double sum = 0;
	int count;

	for (count=1; count <= 5; count++)

	{
		inp >> score;
		outp << left << setw(6) << score;
		sum = sum + score;
		courseAvg = sum / 5;

	}
}

char CalculateGrade(double courseAvg)
{
	if (courseAvg >= 90)
	return 'A';
	else if (courseAvg >= 80)
	return 'B';
	else if (courseAvg >= 70)
	return 'C';
	else if (courseAvg >= 60)
	return 'D';
	else
	return 'F';

	
}


The problem is that the Course Average is reading the very last average, as shown here:

1
2
3
4
5
6
7
8
9
10
11
12
13
Student  Test1 Test2 Test3 Test4 Test5 Average Grade
Johnson    85    83    77    91    76    82.4    B     
Aniston    80    90    95    93    48    81.2    B     
Cooper     78    81    11    90    73    66.6    D     
Gupta      92    82    30    69    87    72.0    C     
Blair      23    45    96    38    59    52.2    F     
Clark      60    85    45    39    67    59.2    F     
Kennedy    77    31    52    74    83    63.4    D     
Bronson    93    94    89    77    97    90.0    A     
Sunny      79    85    28    93    82    73.4    C     
Smith      85    72    49    75    63    68.8    D     

Class Average = 6.9

Do I need to declare another function somewhere?
Last edited on
No, You just have your loops mixed up a little.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (counter = 1; counter <= 10; counter++) {
	infile >> name;
	outfile << left << setw(11) << name;

	CalculateAverage(infile, outfile, courseAvg);

	grade = CalculateGrade(courseAvg);
	
	outfile << setw(8) << courseAvg << setw(6) << grade <<endl;
	
	}

	classAvg = classAvg + courseAvg;
	classAvg = classAvg / 10;


Should be.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
for (counter = 1; counter <= 10; counter++) {
	infile >> name;
	outfile << left << setw(11) << name;

	CalculateAverage(infile, outfile, courseAvg);

	grade = CalculateGrade(courseAvg);
	
	outfile << setw(8) << courseAvg << setw(6) << grade <<endl;
	classAvg  += courseAvg;
	}

	
	classAvg /= 10;


Also, In your CalculateAverage Function

courseAvg = sum / 5;

Should go outside of the For Loop. There no point having it inside.
Last edited on
That worked, thank you very much! It is much appreciated.
No worries.
Topic archived. No new replies allowed.