1: //positive_average.cpp
2: //Finds the average of all positive integer values input by
3: //the user. Illustrates an EOF-controlled loop, and a test
4: //after the loop to make sure the input data set was not empty.
6: #include <iostream>
7: using namespace std;
9: int main()
10: {
11: cout << "\nThis program computes the average of any number of "
12: "positive integer values.\nInput values may be positive, "
13: "negative, or zero, but non-positive values are\nsimply "
14: "ignored. Enter as many values as you like, then press the "
15: "Enter key.\nTo compute and display the avarage enter an "
16: "end-of-file and press Enter again.\n";
18: int i;
19: int numberOfPositiveIntegers = 0;
20: int sum = 0;
21: double average;
23: cout << "\nStart entering values on the line below:\n";
24: cin >> i;
25: while (cin)
26: {
27: if (i > 0)
28: {
29: sum = sum + i;
30: numberOfPositiveIntegers++;
31: }
32: cin >> i;
33: }
35: if (numberOfPositiveIntegers == 0)
36: cout << "No positive integers were input.\n";
37: else
38: {
39: average = double(sum) / double(numberOfPositiveIntegers);
40: cout << "The average of the " << numberOfPositiveIntegers
41: << " positive integer values is " << average << ".\n";
42: }
43: cout << endl;
44: }