/* Write a function that returns an integer number from the standard input strea
ID: 3776967 • Letter: #
Question
/*
Write a function that returns an integer number from the standard input stream. The number is greater
than or equal to the function’s lower bound parameter and is less than or equal to the function’s upper
bound parameter. The function’s third parameter specifies the maximum number of times the function tries
to read a number. This maximum is a positive integer. The function must also handle non-numeric input.
If the function reads a number within the specifies range and within the maximum number of tries then the
function returns true; otherwise the function returns false.
The function’s prototype is
bool readNumber(int lbound, int ubound, int ntries, int& number);
*/
here is my code
#include <iostream>
#include <string>
using namespace std;
bool readNumber(int lbound, int ubound, int ntries, int& number);
int main()
{
int number, ubound, lbound, ntries, wrong;
lbound = 1;
ubound = 10;
ntries = 3;
cout << "Please enter an integer number: ";
cin >> number;
readNumber(lbound, ubound, ntries, number);
if (readNumber(lbound, ubound, ntries, number))
cout << "you win" << endl;
else cout << "you lose"<< endl;
system ("pause");
return 0;
}
bool readNumber(int lbound, int ubound, int ntries, int& number )
{
if(number > lbound && number < ubound)
{
return number = true;
}
for ( int i = 0; i < 3; i++)
{
do
{
if( number <= lbound || number >= ubound)
{
return number = false;
cin.clear();
cin.ignore();
}
} while(number <= lbound || number >= ubound);
}
/* do
{
if( number <= lbound || number >= ubound || wrong < ntries)
{
return number = false;
cin.clear();
cin.ignore();
wrong ++;
}
} while(number <= lbound || number >= ubound || wrong <= ntries);
if( wrong > ntries)
{
cout << "you have reached the limit" << endl;
return number = false;
}*/
}
Explanation / Answer
Please follow the code and comments for description :
CODE :
#include <iostream> // required header files
#include <string>
using namespace std;
bool readNumber(int lbound, int ubound, int ntries, int& number); // function declaration
int main() // driver method
{
int number, ubound, lbound, ntries; // locla variables
lbound = 1;
ubound = 10;
ntries = 3;
cout << "Please enter an integer number : "; // prompt for the user
cin >> number;
if (readNumber(lbound, ubound, ntries, number)) { // check for the returned value
cout << "Yay..!! You WIN." << endl;
} else {
cout << "Alas..!! You LOSE."<< endl;
}
return 0;
}
bool readNumber(int lbound, int ubound, int ntries, int& number ) // function initialisation
{
int wrong = 0; / local variables
if(number > lbound && number < ubound) { // check for the number
return true;
} else {
for ( int i = 0; i < ntries; i++) // iterate over the loop for the maximum tries
{
do
{
cout << "Please enter an integer number : "; // prompt for the user
cin >> number; // get the data
if( number <= lbound || number >= ubound) // check for the user data
{
wrong++; // increment the count
if( wrong >= ntries) // check for the entries
{
cout << "Sorry..!! You have reached the Maximum limit of Tries(3)." << endl; // message for the user
return false;
}
cin.clear();
cin.ignore();
} else if(number > lbound && number < ubound){ //check for the number
return true; // return true
}
} while(number <= lbound || number >= ubound);
}
}
}
OUTPUT :
Case 1 :
Please enter an integer number : 0
Please enter an integer number : 11
Please enter an integer number : 12
Please enter an integer number : 14
Sorry..!!
You have reached the Maximum limit of Tries(3).
Alas..!! You LOSE.
Case 2 :
Please enter an integer number : 0
Please enter an integer number : 15
Please enter an integer number : 2
Yay..!! You WIN.
Hope this is helpful.
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.