Module specs for an input validation function: isValidInt Return Value: true (if
ID: 641940 • Letter: M
Question
Module specs for an input validation function:
isValidInt
Return Value: true (if inputInt is between min and max, inclusive), false otherwise
Reference parameters: (none)
Receives: inputInt (int), min (int), max (int)
Preconditions: inputInt has been read, min and max have been initialized
Logic: Return true if inputInt is >= min and <= max, false otherwise
Write the function in C++ for the isValidInt function
Then, test the function in a program (you may loop if isValidInt returns false, but at least use an if statement).
Explanation / Answer
#include "ddcpp.h" string UserInput::GetString() { string Input = ""; while (Input == "") { getline (cin, Input);} return Input; } int UserInput::GetInt(int NumberOfChoices) { string Input = ""; int MaxNumber = 0; int IsValidInt = 0; while (IsValidInt == 0){ Input = ""; IsValidInt = 0; MaxNumber = NumberOfChoices; while (Input == "") { getline (cin, Input); } IsValidInt = ValidateInt (Input, MaxNumber); if (IsValidInt == 0) { cout MaxValidNumber) || (InputInt < 0)) { Valid = 0; } if ((InputInt 0)) { Valid = 1; ValidInt = InputInt; } } } else { Valid = 0; } } return Valid; } bool UserInput::ValidateInput(int Input) { int InputInt = Input; int Choice = 0; bool Correct = false; while(Correct == false) { coutRelated Questions
Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.