I am only one week into programming and I'm trying to learn how to use a loop. I need to ask the user to enter in 5 different test scores, and instead of repeating the step 5 times (what I did below), how can I make a loop to do it for me?
#include <iostream>
int main()
{
constint NSCORES = 5 ;
double total_score = 0 ;
for( int i = 0 ; i < NSCORES ; ++i ) // repeat NSCORES times; for i = 0, 1, 2, .... (NSCORES-1)
{
std::cout << "Please enter test_" << i+1 << " score: " ;
double score ;
std::cin >> score ; // read the score of this test
// assume that we are not required to validate the score; for example, score must be in the range (0,100)
total_score += score ; // add to total
}
constdouble average_score = total_score / NSCORES ; // compute average
std::cout << "average score: " << average_score << '\n' ;
}