/* testfunc04.cpp CIS 150 2008-06-10 David Klick This program demonstrates how to create an absolute value function for integers. */ #include #include #include // for toupper using std::cin; using std::cout; void cont(); int absval(int x); // function prototype int main(int argc, char* argv[]) { char choice; int num1; do { // get value to convert from user cout << "Enter an integer: "; cin >> num1; // get absolute value and display it cout << "The absolute value of " << num1 << " is " << absval(num1) << '\n'; // find out if user wants to keep going do { cout << "\nCalculate another value? (Y/N) "; cin >> choice; choice = toupper(choice); // check for invalid user entry if (choice != 'Y' && choice != 'N') { cout << "Error: Invalid choice\n"; } } while (choice != 'Y' && choice != 'N'); } while (choice == 'Y'); cout << "\nNormal program termination\n"; cont(); return 0; } /* int absval(int a); Arguments: int a: an arbitrary integer value Returns: the absolute value of the argument This function calculates and returns the absoulute value of an integer. */ int absval(int a) { return a>=0 ? a : -a; } /* void cont(); Arguments: none Returns: nothing Clears input buffer and pauses waiting for user to press Enter. */ void cont() { if (cin.rdbuf()->sungetc() != -1 && cin.get() != '\n') cin.ignore(80,'\n'); cout << "Press enter to continue..."; cin.get(); }