On line 11 you declare
results to be of type
int. On line 12 you try to treat
results as if it was an array. If it were an array, the line would still be nonsensical - both
i and
getScore have indeterminate values, so you would likely be accessing memory you don't own and assigning some junk value to it.
Line 16 is the body of the for loop. None of the rest of the code is governed by it. The
i defined in the for loop hides the
i defined before the for loop. If
results were an array of 10 elements and your intention was to access each element, valid indexes are 0 to 9, so your for condition would be wrong. Of course, that isn't what your for loop does. Right now all it would do (if it compiled) is print "Please enter ten test scores from your class" eleven times.
If lines 18 to 37 were governed by your for loop, they would each be repeated 11 times, prompting you to enter 110 scores and attempting to store all of them in the same double variable (resulting in the variable being overwritten 109 times with all but the last score lost.)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24
|
#include <iostream>
int main()
{
const std::size_t size = 10;
double scores[size] = {};
// display the values in scores:
for (std::size_t i = 0; i < size; ++i)
std::cout << scores[i] << ' ';
std::cout << '\n';
// get new values for scores:
for (std::size_t i = 0; i < size; ++i)
{
std::cout << "Score " << i + 1 << ": ";
std::cin >> scores[i];
}
// display the new values:
for (std::size_t i = 0; i < size; ++i)
std::cout << scores[i] << ' ';
std::cout << '\n';
}
|