Respuesta :
Complete Question:
Write a method getIntVal that will get the correct value input as integer numbers from the user. The input will be validated based on the first two numbers received as parameters.
In other words, your program will keep asking for a new number until the number that the user inputs is within the range of the <firstParameter> and <secondParameter>.
The method should present a message asking for the value within the range as:
Please enter a number within the range of (<firstParameter> and <secondParameter>):
Note that <firstParameter> should be changed by the value received as that parameter and <secondParameter> as well.
If the user inputs a value that it is lower than the first value, the program will show the message:
The input number is lower than <firstParameter>
Note that <firstParameter> should be changed by the value received as that parameter
If the user inputs a value that it is greater than the first value, the program will show the message:
The input number is greater than <secondParameter>
Note that <secondParameter> should be changed by the value received as that parameter.
You do not need to modify anything in the main method, you just need to write the missing parts of your new getIntVal method.
Answer:
#include<iostream>
using namespace std;
void getIntVal(int num1, int num2) {
int num;
cout<<"Please enter a number within the range of "<<num1<<" and "<<num2<<": ";
cin>>num;
while(num<num1 || num>num2) {
if(num<num1) {
cout<<"The input number is lower than "<<num1<<endl;
}
if(num>num2) {
cout<<"The input number is greater than "<<num2<<endl;
}
cout<<"Please enter a number within the range of "<<num1<<" and "<<num2<<": ";
cin>>num;
}
cout<<"Output: "<<num;
}
int main() {
int num1,num2;
cout<<"Enter lower bound: ";
cin>>num1;
cout<<"Enter upper bound: ";
cin>>num2;
getIntVal(num1, num2);
return 0;
}
Explanation:
Programming Language is not stated, So, I answered using C++
I've added the full source code as an attachment where I use comments to explain difficult lines
