Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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" << endl; cout << "9abc" << endl; cout << "9 (there is a space after 9)" << endl; cout << "9" << endl; cout << getInt("> ") << endl << endl;

cout << "Enter the following values at the prompt: " << endl; cout << "abc" << endl; cout << "9" << endl; cout << "10" << endl; cout << "21" << endl; cout << "21 (there is a space after 21)" << endl; cout << "20" << endl;

}

tester output

getInt tester: Enter 11: 11 11 Enter the following values at the prompt: abc 9abc 9  (there is a space after 9) 9 > abc Bad integer value, try again: 9abc Enter only an integer, try again: 9 Enter only an integer, try again: 9 9 Enter the following values at the prompt: abc 9 10 21 21  (there is a space after 21) 20 > abc Bad integer value, try again: 9 Value must be between 10 and 20: 10 > 21 Invalid value, retry [10 <= value <= 20]: 21 Enter only an integer, try again: 20 last value entered: 20 

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Flash XML Applications Use AS2 And AS3 To Create Photo Galleries Menus And Databases

Authors: Joachim Schnier

1st Edition

0240809173, 978-0240809175

More Books

Students also viewed these Databases questions

Question

Analyse the process of new product of development.

Answered: 1 week ago

Question

Define Trade Mark.

Answered: 1 week ago

Question

What is the Definition for Third Normal Form?

Answered: 1 week ago

Question

Provide two examples of a One-To-Many relationship.

Answered: 1 week ago