The average program is similar to the maximum score program. It's important to initialize count
and sum
as values are added or accumulated in them. score
is not initialized when it is defined because it is initialized with an input statement before entering the while is entered.
#include <iostream> using namespace std; int main() { int score; int count = 0; double sum = 0; cout << "Enter a score (-1 to end): "; cin >> score; while (score != -1) { count++; sum += score; cout << "Enter a score (-1 to end): "; cin >> score; } cout << "The average is " << sum / count << endl; return 0; }