/* testfunc09.cpp CIS 150 2008-06-10 David Klick This program demonstrates function overloading. */ #include using std::cin; using std::cout; int getInt(); int getInt(char* prompt); int getInt(char* prompt, int min, int max); void cont(); int main(int argc, char* argv[]) { cout << "You entered: " << getInt() << '\n'; cout << "You entered: " << getInt("Enter an integer: ") << '\n'; cout << "You entered: " << getInt("Enter an integer (10-20): ", 10, 20) << '\n'; cont(); return 0; } /* int getInt(); Arguments: none Returns: an integer entered by the user Prompts user for integer and returns value entered. Note: For simplicity, no error checking on input is done, therefore this function may crash the program on invalid input. */ int getInt() { int num; cout << "Please enter an integer: "; cin >> num; return num; } /* int getInt(char* msg); Arguments: msg: a pointer to a C-style string to be used as a prompt Returns: an integer entered by the user Prompts user with message passed in as argument and returns the integer value entered. Note: For simplicity, no error checking on input is done, therefore this function may crash the program on invalid input. */ int getInt(char* msg) { int num; cout << msg; cin >> num; return num; } /* int getInt(char* msg, int min, int max); Arguments: msg: a pointer to a C-style string to be used as a prompt min: the minimum value allowed to be entered max: the maximum value allowed to be entered Returns: an integer entered by the user Prompts user with message passed in as argument and returns the integer value entered. The number must be between min and max (inclusive) or the user will be reprompted to enter another number. Note: For simplicity, no error checking on input is done, therefore this function may crash the program on invalid input. */ int getInt(char* msg, int min, int max) { int num; do { cout << msg; cin >> num; if (num < min) cout << "Error: value below minimum allowed\n"; else if (num > max) cout << "Error: value above maximum allowed\n"; } while (nummax); return num; } /* 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(); } /* Sample output: Please enter an integer: -30 You entered: -30 Enter an integer: -30 You entered: -30 Enter an integer (10-20): -30 Error: value below minimum allowed Enter an integer (10-20): 100 Error: value above maximum allowed Enter an integer (10-20): 17 You entered: 17 */