/* testrep4.cpp CIS 150 6/10/2008 David Klick Demonstration of accumulator (for sum). */ #include using std::cin; using std::cout; void cont(); int main() { int ctr; // loop counter int sum; // accumulator // add up all the numbers from 1 to 10 using for sum = 0; for (ctr=1; ctr<=10; ctr++) { sum += ctr; } cout << "The sum of 1 through 10 is " << sum << '\n'; // add up all the numbers from 1 to 10 using while sum = 0; ctr = 1; while (ctr<=10) { sum += ctr; ctr++; } cout << "The sum of 1 through 10 is " << sum << '\n'; // add up all the numbers from 1 to 10 using do/while sum = 0; ctr = 1; do { sum += ctr; ctr++; } while (ctr <= 10); cout << "The sum of 1 through 10 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: The sum of 1 through 10 is 55 The sum of 1 through 10 is 55 The sum of 1 through 10 is 55 */