// averageScores.cpp
// Averages a sequence of exam scores entered by the user.
// The scores are in the range 0 to 100.
// The user signals end of the sequence by typing -1.

#include <iostream>
using namespace std;

int main()
{
   // Announce purpose of program:
   cout << "This program averages a sequence of exam scores "
             << "entered by the user."  << endl;
   cout << "The scores must be in the range 0 to 100." << endl;

   // Cumulative sum and count of entered numbers:
   int sum = 0;
   int count = 0;

   // Input and error-check the FIRST score:
   cout << "Enter a score (or -1 to quit):>";
   int score;
   cin >> score;
   if ( !cin || score < -1 || score > 100 )
   {
      cout << "Not a valid score." << endl;
      return 1;
   }  // if

   // Input more scores to be averaged, and
   // add them all to cumulative sum, until
   // user enters -1 to signal no more scores.
   while ( score != -1 )
   {
      // Add user entry to cumulative sum:
      sum = sum + score;

      // Increment count of scores entered so far:
      count++;

      // Input and error-check the NEXT score:
      cout << "Enter a score (or -1 to quit):>";
      cin >> score;
      if ( !cin || score < -1 || score > 100 )
      {
         cout << "Not a valid score." << endl;
         return 1;
      }  // if
   }  // while

   // Prevent division by zero:
   if ( count == 0 )
   {
      cout << "You didn't enter any scores." << endl;
      return 1;
   }  // if

   // Compute the average and output the result:
   float average = (float) sum / count;
   cout << "The average is: " << average << "." << endl;

   return 0;
}  // function main