// This program illustrates a sentinel controlled while loop. // Write a program that calculates the average of an unknown number of positive integers. // The program is to stop when a negative one (-1) is entered. #include using namespace std; int main() { int value, count, sum = 0; count = 0; cout << "Please enter positive integers, a -1 to stop" << endl; cin >> value; // initialize //test to make sure an invalid number (negative other than -1) was not entered while (value < -1) { cout << " only positive numbers will be used - remember -1 to stop" << endl; cin >> value; } // if positive value add to the sum while (value != -1) // test { sum += value; count++; cin >> value; // modify // test for invalid number while (value < -1) { cout << " only positive numbers will be used - remember -1 to stop" << endl; cin >> value; } } // calculate average - only if some data to average if (count > 0) cout << " The average is " << (double)sum / count << endl; else cout << " No average calculated - no values entered " << endl; }