I am running into a baffling problem with my code. I am supposed to write a program to average the test scores for an entire class of students. In each case, the student should have taken 5 tests. You are to average the 5 tests.
The program run should look like this.
How many students are in the class ?
3

Enter five test scores for student number 1 90 90 70 90 80 The average for student number 1 is 84

Enter five test scores for student number 2 100 60 60 90 80 The average for student number 2 is 78

Enter five test scores for student number 3 90 70 50 70 90 The average for student number 3 is 74

Here is what I have:

#include <iostream>
#include <iomanip>

using namespace std;


void handleOneStudent(int N);

int main()
{	
	int NumberOfStudents;
	cout << "How many students are in the class ?" << endl;
	cin >> NumberOfStudents;
	cout << endl;
	for (int i=1; i <= NumberOfStudents; i++)
		handleOneStudent(i);
	return 0;
}
void handleOneStudent(int N)
{
	const int num_quizzes = 5;
	int score[num_quizzes];
	double average;

	cout << setprecision(2)
		 << setiosflags(ios::fixed)
		 << setiosflags(ios::showpoint);
	
	cout << "Enter five test scores for student number " << N << endl;
	cin >> score[num_quizzes];
		
	average = (score[1] + score[2] + score[3] + score[4] + score[5])/5;
	cout << endl << endl;

	cout << "The average for student number " << N << " is " << average << endl;
	
}

When I run the code I get the following error:
Error message:

Debug error!

Run-Time Check Failure #2 - Stack around the variable 'score' was corrupted.

Easy, arrays are 0 based.
average = (score[0] + score[1] + score[2] + score[3] + score[4])/5;

cin >> score[num_quizzes];

num_quizzes should never be used as an index for the array as it is out of bounds and will cause a run time error. If the above was an attempt to put all five scores into the array in one call to cin that's an error, too. You need to enter each elements value with a separate call to cin (or maybe a call to memset or some such "exotic" method) whether you write code for each element separately or use a loop to abstract things a bit.

Thanks for the tip. That was what I needed to do amoung a few other things.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.