// average4A.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.
#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 first number:
int number;
cin >> number;
// Input more numbers to be averaged,
// and add them all to cumulative sum,
// until user enters a non-numeric
// character to signal no more numbers:
while ( cin )
{
// Add user entry to cumulative sum:
sum += number;
// Increment count of numbers entered so far:
count++;
// Input the NEXT number:
cin >> number;
} // 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