Using C++ Note that not every function has to have parameters. For example, you
ID: 3569090 • Letter: U
Question
Using C++
Note that not every function has to have parameters. For example, you have been creating the
function main since the beginning, and it is a function without parameters. Using this
information, write a function called ask that asks the user for a number between 1 and 10
(inclusive). Use the following code in main to test it:
int num = 0;
while ((num < 1) || (num > 10)) {
num = ask();
}
cout << "You selected " << num << endl;
return 0;
Sample output:
Please enter a number between 1 and 10 (inclusive): 11
Please enter a number between 1 and 10 (inclusive): 0
Please enter a number between 1 and 10 (inclusive): -11
Please enter a number between 1 and 10 (inclusive): 1111
Please enter a number between 1 and 10 (inclusive): 10
You selected 10
Explanation / Answer
#include<iostream>
using namespace std;
int ask()
{
int i;
cout << "Please enter a number between 1 and 10 (inclusive):" ;
cin >> i;
return i;
}
int main()
{
int num = 0;
while ((num < 1) || (num > 10)) {
num = ask();
}
cout << "You selected " << num << endl;
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.