Calculating Average

I need to have it stop after I enter the 10th score. This is what I have so far.

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
#include <iostream>
using namespace std;

int main ()
{
    double scores = 0.0;
    double totalScores = 0.0;
    double average = 0.0;
    
    cout << "Enter score: ";
    cin >> scores;
    
    for (int students = 1; students <= 10; students += 1)
    {
              
        totalScores = totalScores + scores;
        average = totalScores / students;
        cout << "Enter next score: ";
        cin >> scores;
        cout << "The average is: " << average << endl;
    }
    
    system("pause");
    return 0;
}
First, get rid of the inputting of scores that takes places before the loop; you might as well do it all in the loop.

You can only know the average after you know all the scores, so don't output the average inside the loop; do it after. Right after you get each score inside the loop, add it to the totalScores. After the loop, you can calculate the average just with totalScores/10.
Topic archived. No new replies allowed.