/* testrep9.cpp CIS 150 6/7/2005 David Klick Demonstration of priming read. This program adds up the numbers a user enters. One problem is that the sentinel value used eliminates one possible integer (-999) that the user may want to enter. */ #include using std::cout; using std::cin; int main(void) { int num; // variable for user input int sum; // accumulator // display informational message and reset sum cout << "Adding machine program\n"; sum = 0; // get first input from user (priming read) cout << "Enter an integer (-999 to exit): "; cin >> num; // keep going through loop until user enters -999 while (num != -999) { // add user entry to accumulator sum += num; // get next user entry cout << "Enter an integer (-999 to exit): "; cin >> num; } // display result cout << "The sum is " << sum << '\n'; return 0; } /* Sample output: Adding machine program Enter an integer (-999 to exit): 25 Enter an integer (-999 to exit): 15 Enter an integer (-999 to exit): -10 Enter an integer (-999 to exit): -999 The sum is 30 */