/* testrep5.cpp CIS 150 6/10/2008 David Klick Demonstration of accumulator (for product). */ #include using std::cout; using std::cin; void cont(); int main() { int ctr; // loop counter int fact; // accumulator for factorial int num; // number user enters // ask user for number to get factorial of cout << "Enter an integer (1 through 10): "; cin >> num; if (num<1 || num>10) { cout << "Error: Input out of range\n"; } else { // calculate factorial fact = 1; for (ctr=2; ctr<=num; ctr++) { fact *= ctr; } cout << "The factorial of " << num << " is " << fact << '\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: Enter an integer (1 through 10): 7 The factorial of 7 is 5040 */