Why is this outputting zero's?!?!?!

I have written a program to input test/program grades from multiple students to calculate various average's. They are weighted: tests are 80% and programs are 20%. The program works perfectly except that the final 3 average's are not calculating correctly. Here is some test data:

Number of Tests: 5
Number of Programs: 3

Name Test Grades Program Grades
Myles 80,75,65,90,88 94,85,89
Garrett 90,72,87,96,79 71,72,82

Here is the code:
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
#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>

using namespace std;

string courseName, studentName;

int testlimit, programlimit, studentlimit;

float testGrade, programGrade;
float studentavg;
float studenttestavg, studentprogramavg, finalstudentavg;

char lettergrade;

ofstream outData;

int main()
{
	outData.open("grades.dat");
	
	cout << "What is the course name?" << endl;
	cin >> courseName;
	
	cout << "How many students are there?" << endl;
	cin >> studentlimit;
	
	cout << "How many tests are there?" << endl;
	cin >> testlimit;
	
	cout << "How many programs are there?" << endl;
	cin >> programlimit;
	
	int studentcounter = 1;
	
	outData << setw(10) << left << setfill(' ') << "Name";
	outData << setw(15) << left << setfill(' ') << "Test Average";
	outData << setw(17) << left << setfill(' ') << "Program Average";
	outData << setw(15) << left << setfill(' ') << "Final Average";
	outData << setw(15) << left << setfill(' ') << "Letter Grade";
	outData << endl;
	
	float testsum = 0;
	float programsum = 0;
	
	while(studentcounter <= studentlimit)
	{
		
		cout << "What is the student's name?" << endl;
		cin >> studentName;
		
		int testcounter = 1;
		int programcounter = 1;
	
		while(testcounter <= testlimit || programcounter <= programlimit)
		{	
			float testsum = 0;
			float programsum = 0;
			while(testcounter <= testlimit)
			{
				cout << "What is the test grade?" << endl;
				cin >> testGrade;
				testsum = testsum + testGrade;
				studenttestavg = testsum / testlimit;
				testcounter++;
			}
			while(programcounter <= programlimit)
			{
				cout << "What is the program grade?" << endl;
				cin >> programGrade;
				programsum = programsum + programGrade;
				studentprogramavg = programsum / programlimit;
				programcounter++;
			}
		}
		studentcounter++;
		
		finalstudentavg = (studenttestavg * .8) + (studentprogramavg * .2);
	
	if(finalstudentavg >= 90)
	{
		lettergrade = 'A';
	}
	else if(finalstudentavg >= 80 && finalstudentavg < 90)
	{
		lettergrade = 'B';
	}
	else if(finalstudentavg >= 70 && finalstudentavg < 80)
	{
		lettergrade = 'C';
	}
	else if(finalstudentavg >= 60 && finalstudentavg < 70)
	{
		lettergrade = 'D';
	}
	else if(finalstudentavg < 60)
	{
		lettergrade = 'F';
	}
		
		setprecision(0);
		outData << endl;
		outData << setw(10) << left << setfill(' ') << studentName;
		outData << setw(15) << left << setfill(' ') << studenttestavg;
		outData << setw(17) << left << setfill(' ') << studentprogramavg;
		outData << setw(15) << left << setfill(' ') << finalstudentavg;
		outData << setw(15) << left << setfill(' ') << lettergrade;
		
	}
	
	float testpointspossible = testlimit * 100;
	float programpointspossible = programlimit * 100;

	float testclassavg = testsum / testpointspossible;
	float programclassavg = programsum / programpointspossible;
	float overallclassavg = ((testclassavg * .8) + (programclassavg * .2));
	
	outData << endl << endl;
	outData << "The class test average is " << testclassavg << '.';
	outData << endl;
	outData << "The class program average is " << programclassavg << '.';
	outData << endl;
	outData << "The overall class average is " << overallclassavg << '.';
	
	outData.close();
	
	return 0;
}
try separating out the while loop you had for the test and program input.

See what that does.

And why are all your variables global?
Exactly what do you mean by separating them out. I forgot to mention that I am in a Foundations of Programming class. So I am a beginner. What exactly does global mean, as far as variables are concerned? I look forward to learning from your input. Thank you.

Why global variables are evil:
http://www.learncpp.com/cpp-tutorial/42-global-variables/

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
135
136
137
138
139

#include <iostream>
#include <iomanip>
#include <string>
#include <fstream>
using namespace std;



int main()
{

       // Variable Declarations
       string courseName, studentName;

       int testlimit, programlimit, studentlimit;

       float testGrade, programGrade;
       float studentavg;
       float studenttestavg, studentprogramavg, finalstudentavg;

       char lettergrade;

       ofstream outData;


	outData.open("grades.dat");
	
	cout << "What is the course name?" << endl;
	cin >> courseName;
	
	cout << "How many students are there?" << endl;
	cin >> studentlimit;
	
	cout << "How many tests are there?" << endl;
	cin >> testlimit;
	
	cout << "How many programs are there?" << endl;
	cin >> programlimit;
	

	outData << setw(10) << left << setfill(' ') << "Name";
	outData << setw(15) << left << setfill(' ') << "Test Average";
	outData << setw(17) << left << setfill(' ') << "Program Average";
	outData << setw(15) << left << setfill(' ') << "Final Average";
	outData << setw(15) << left << setfill(' ') << "Letter Grade";
	outData << endl;
	
	float testsum = 0;
	float programsum = 0;
	float classTestSum = 0; // Declared a new variable here to add the testsum values of each student.
	float classProgramSum =0; // Declared a new variable here to add the programsum values of each student.
	
	
	int studentcounter = 0;	
	while(studentcounter < studentlimit)
	{
		
		cout << "What is the student's name?" << endl;
		cin >> studentName;
		
		int testcounter = 0;
		int programcounter = 0;
		float testsum = 0;
		float programsum = 0;
		
		while(testcounter < testlimit)
		{
			cout << "What is the test grade?" << endl;
			cin >> testGrade;
			testsum = testsum + testGrade;
			studenttestavg = testsum / testlimit;
			testcounter++;
		}
		while(programcounter < programlimit)
		{
			cout << "What is the program grade?" << endl;
			cin >> programGrade;
			programsum = programsum + programGrade;
			studentprogramavg = programsum / programlimit;
			programcounter++;
		}
		studentcounter++;
		
		finalstudentavg = (studenttestavg * .8) + (studentprogramavg * .2);
	
		if(finalstudentavg >= 90)
		{
			lettergrade = 'A';
		}
		else if(finalstudentavg >= 80 && finalstudentavg < 90)
		{
			lettergrade = 'B';
		}
		else if(finalstudentavg >= 70 && finalstudentavg < 80)
		{
			lettergrade = 'C';
		}
		else if(finalstudentavg >= 60 && finalstudentavg < 70)
		{
			lettergrade = 'D';
		}
		else if(finalstudentavg < 60)
		{
			lettergrade = 'F';
		}
		
		setprecision(0);
		outData << endl;
		outData << setw(10) << left << setfill(' ') << studentName;
		outData << setw(15) << left << setfill(' ') << studenttestavg;
		outData << setw(17) << left << setfill(' ') << studentprogramavg;
		outData << setw(15) << left << setfill(' ') << finalstudentavg;
		outData << setw(15) << left << setfill(' ') << lettergrade;
		
		classTestSum = classTestSum + testsum;  // add the testsum variables
		classProgramSum = classProgramSum + programsum;  // add the programsum variables
		
	}

       // With this new information, you are able to compute the test and program scores
       // efficiently.
	
	float testclassavg = (classTestSum / ((testlimit * studentlimit)*100)) * 100;
	float programclassavg = (classProgramSum / ((programlimit * studentlimit)*100)) * 100;
	float overallclassavg = ((testclassavg * .8) + (programclassavg * .2));
	
	outData << endl << endl;
	outData << "The class test average is " << testclassavg << '.';
	outData << endl;
	outData << "The class program average is " << programclassavg << '.';
	outData << endl;
	outData << "The overall class average is " << overallclassavg << '.';
	
	outData.close();
	
	return 0;
}
Wow. Thank you. I did not expect you to re-write it, but thank you. I will definitely read on the global variables though. I just glanced at the page and from what I read I can tell you that the reason most of my variables are global is because this class is to introduce thinking in a "programmer's" perspective. To get to thinking about algorithm's and logic. But once again, thank you.
What's with all the endls? You surely don't need to flush the output buffer four times? Replace those endls with "\n"s instead and your code will be faster.
That's just the easiest and fastest way my mind looks at code. I have noticed in my past 3 labs that "\n" is very easy to use, so I do plan on using it in the future. Thank you though for letting me know it runs faster. I did not know this. But, of course, I'm in a beginning programming course and we are not yet dealing with large programs. Thank you once again.
I should add, that you most probably won't notice a difference with that amount of them. But in a large program that inexplicably uses standard streams, it's a little faster.
Topic archived. No new replies allowed.