/* testfunc04.cpp CIS 150 6/9/2005 David Klick This program demonstrates how to create an absolute value function for integers. */ #include #include #include // for toupper using std::cout; using std::cin; int absval(int x); // function prototype int main(void) { 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"; return 0; } /* This function calculates and returns the absoulute value of an integer. */ int absval(int a) { return a>=0 ? a : -a; }