Question
hi, could u plz fix my function? This function performs a fool-proof integer entry from the console. If the prompt is not null, it is
hi, could u plz fix my function?
This function performs a fool-proof integer entry from the console.
If the prompt is not null, it is displayed before the entry as a prompt (only once).
If the user enters an invalid integer, the message "Bad integer value, try again: " is displayed until the user enters a valid integer value.
If after a valid integer value any character other than a New Line is entered, then the message "Enter only an integer, try again: " is displayed until the user enters only an integer value at the entry.
The entered value is returned at the end.
my implementation
int getInt(const char* prompt) { if (prompt) cout << prompt< bool done = false; int num; while (!done) { cin >> num; if (cin.fail()) { cin.clear(); cin.ignore(3000, ' '); cout << "Bad integer value, try again: "; } else done = true; } while(getchar() != ' ') cout << "Enter only an integer, try again: "; return num; } test Unit int man(){ cout << "getInt tester:" << endl; cout << "Enter 11: "; cout << getInt() << endl << endl; cout << "Enter the following values at the prompt: " << endl; cout << "abc cout << "Enter the following values at the prompt: " << endl; cout << "abc } tester output getInt tester: Enter 11: 11 11 Enter the following values at the prompt: abc
Step by Step Solution
There are 3 Steps involved in it
Step: 1
Get Instant Access to Expert-Tailored Solutions
See step-by-step solutions with expert insights and AI powered tools for academic success
Step: 2
Step: 3
Ace Your Homework with AI
Get the answers you need in no time with our AI-driven, step-by-step assistance
Get Started