Look at this fragment that is in your code:
1 2 3 4
|
for ( int i = 0; i < NUM_TESTS; i++ )
{
cin >> students[ i ].testScores[ NUM_TESTS ];
}
|
Lets pretend that the
testScores[ NUM_TESTS ]
is named
attribute
The code looks like:
1 2 3 4
|
for ( int i = 0; i < NUM_TESTS; i++ )
{
cin >> students[ i ].attribute;
}
|
What happens there?
* NUM_TESTS values are read from the user
* Each value is stored in attribute of a different student
That is all fine, as long as the array
students
has at least NUM_TESTS elements. Does it?
Lets not pretend any more.
A
Student
has member array
testScores
.
How many elements does that array have?
NUM_TESTS elements.
Is
testScores[ NUM_TESTS ]
an element in the array
testScores
? No.
The last element of the array is
testScores[ NUM_TESTS - 1 ]
It is an
out of range error to do:
1 2
|
Student john;
cin >> john.testScores[ NUM_TESTS ]; // ERROR
|
You do that error NUM_TESTS times in that loop. If you call that loop more than once, then you repeat the error even more.
Furthermore, you never stored anything into any element of the array testScores. Why do you have an array that you don't use for anything?
Yes, you should know what indices you use.