// average4C.cpp
// Averages a sequence of integers entered by the user.
// The user signals end of the sequence by typing a
// non-numeric character, such as 'X'.
// This program follows the rules of structured programming.
// Consolidates input statement with test of state of input stream.

#include <iostream>
using namespace std;

int main()
{
   // Announce purpose of program:
   cout << "This program averages a sequence of integers "
             << "entered by the user." << endl;
   cout << "When finished entering numbers, enter a non-numeric "
             << "character, such as X." << endl << endl;

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

   // Prompt user to enter numbers:
   cout << "Enter the numbers (and then X to quit):> ";

   // Input the numbers to be averaged,
   // and add them all to cumulative sum,
   // until user enters a non-numeric
   // character to signal no more numbers:
   int number;
   while ( cin >> number )
   {
      // Add user entry to cumulative sum:
      sum += number;

      // Increment count of numbers entered so far:
      count++;
   }  // while

   // Prevent division by zero:
   if ( count == 0 )
   {
      cout << "You didn't enter any numbers." << 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