/* testrep9.cpp CIS 150 6/10/2008 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; void cont(); int main() { 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'; cont(); return 0; } void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); } /* 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 */