end of input file, partially correct output

Hey! Thanks for looking at this. I have an assignment that inputs data and outputs to console an average, etc. Everything works correct with one exception- I have about 20 or so lines of garbage after what is supposed to be the end of my output. After the for loop that gives a count that is used to output the data in the last loop of the code, I output the count, which turns out, in this case, to be 26. Not entirely sure how I pulled that one off.
I've been doing well at looking at things, trying to get them figured out on my own, but I'm stuck on this one.
this is the sample input file:
1234 78.6 89.7 76.5
2345 90 92.5 91.3
3456 67.2 58.5 43.7
1212 85.4 80 78.6
1312 73.1 80.2 71.5
1789 56.8 68 60
2100 54 65 57
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
  #include <iostream>
#include <iomanip>
#include <fstream>

using namespace std;
double studentAverage(double test_average, double homework_average,
		double quiz_average) {
	double student_average;
	student_average = (((test_average / 100) * .6)
			+ ((homework_average / 100) * .2) + ((quiz_average / 100) * .2))
			* 100;
	return student_average;
}
void displayAverage(int student_id, double student_average) {

	if (student_average < 59.5) {
		cout << "Student " << student_id << " has a course average of "
				<< student_average << " and has received a grade of F.";
	}
	if (student_average >= 59.5 && student_average < 69.5) {
		cout << "Student " << student_id << " has a course average of "
				<< student_average << " and has received a grade of D.";
	}
	if (student_average >= 69.5 && student_average < 79.5) {
		cout << "Student " << student_id << " has a course average of "
				<< student_average << " and has received a grade of C.";
	}
	if (student_average >= 79.5 && student_average < 89.5) {
		cout << "Student " << student_id << " has a course average of "
				<< student_average << " and has received a grade of B.";
	}
	if (student_average >= 89.5 && student_average <= 100) {
		cout << "Student " << student_id << " has a course average of "
				<< student_average << " and has received a grade of A.";
	}
}
int main() {
	ifstream input_file("class_grades.txt");
	const int MAX_STUDENTS = 25;
	int student_id[MAX_STUDENTS];
	double test_average[MAX_STUDENTS];
	double homework_average[MAX_STUDENTS];
	double quiz_average[MAX_STUDENTS];
	int count = 0;
	if (!input_file)
	{
		cout << "There is no input file. Goodbye.";
	}else
	while (!input_file.eof() && count < MAX_STUDENTS) {
		for (int student = 0; student <= MAX_STUDENTS; student++) {
			input_file >> student_id[student] >> test_average[student]
					>> homework_average[student] >> quiz_average[student];
			count++;
		} cout << count << endl; //the count comes out to be 26 which causes the array to fill up
		//with garbage.  How does that happen, given the arguments of the loop?
		int student_average;

		for (int index = 0; index <= count; index++) {
			student_average = studentAverage(test_average[index],
					homework_average[index], quiz_average[index]);
			displayAverage(student_id[index], student_average);
			cout << endl;
		}
	}
}

and here's the output:
26
Student 1234 has a course average of 80 and has received a grade of B.
Student 2345 has a course average of 90 and has received a grade of A.
Student 3456 has a course average of 60 and has received a grade of D.
Student 1212 has a course average of 82 and has received a grade of B.
Student 1312 has a course average of 74 and has received a grade of C.
Student 1789 has a course average of 59 and has received a grade of F.
Student 2100 has a course average of 56 and has received a grade of F.
Student 6 has a course average of -2.14748e+09 and has received a grade of F.
Student 0 has a course average of -2.14748e+09 and has received a grade of F.
Student 256 has a course average of 0 and has received a grade of F.
Student -2147449766 has a course average of -2.14748e+09 and has received a grade of F.
Student 1 has a course average of -2.14748e+09 and has received a grade of F.
Student 0 has a course average of -2.14748e+09 and has received a grade of F.
Student 10 has a course average of -2.14748e+09 and has received a grade of F.
Student -2147317400 has a course average of 0 and has received a grade of F.
Student 2665496 has a course average of -2.14748e+09 and has received a grade of F.
Student 2664810 has a course average of -2.14748e+09 and has received a grade of F.
Student 2664568 has a course average of -2.14748e+09 and has received a grade of F.
Student 0 has a course average of 0 and has received a grade of F.
Student 2664568 has a course average of -2.14748e+09 and has received a grade of F.
Student 842346803 has a course average of 0 and has received a grade of F.
Student 892745528 has a course average of -2.14748e+09 and has received a grade of F.
Student -2147471047 has a course average of 0 and has received a grade of F.
Student -2147482576 has a course average of -2.14748e+09 and has received a grade of F.
Student 0 has a course average of -2.14748e+09 and has received a grade of F.
Student 1717986918 has a course average of 69 and has received a grade of D.
Student 1079223910 has a course average of 73 and has received a grade of C.

Thanks for the help!
 
const int MAX_STUDENTS = 25;

Within your while-loop, you have a for-loop initiated as follows:

 
for (int student = 0; student <= MAX_STUDENTS; student++)

You have a counter to check the iterations of this for loop which is incremented within the for-loop.

1
2
3
4
5
6
7
for (int student = 0; student <= MAX_STUDENTS; student++){
    /*
    // Lots of other code
    */

    count++; // Right here
}

The for-loop runs until 'student' equals 26, which is more than 'MAX_STUDENTS'. 'count' also hits 26.
The problem comes from the initialization of 'MAX_STUDENTS'. If it equals 25 and there are not 25 students' data in the file, you will output garbage.

To fix this, int MAX_STUDENTS = 20; and change your for-loop to
for(int student = 0; student < MAX_STUDENTS; student++).
- or -
Change the file to contain a 'MAX_STUDENT' number of students' data and change your for-loop to
for(int student = 0; student < MAX_STUDENTS; student++)

You need to change your for-loop regardless. Make '<=' to just '<'

I hope this words for you!
Topic archived. No new replies allowed.