basic function question

Note that not every function has to have parameters. . Using this information, write a function called ask that asks the user for a number between 1 and 10 (inclusive). I have the program 99% accurate. The only problem is, and i know its an easy fix (i think), that when the number is in the good range (between 1 and 10) my code still displays the text "please follow directions" when it should not not even though i specified it too only when the number in an invalid choice. My code is as follows below.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
in ask() {
int num;
cin>> num;
int ask;
ask=num;
return ask;
}
int main () {
int num=0;
while (( num<1) || (num>10)) {
num=ask();
cout<< endl;
cout<< Please enter a number from 1 to 10: ";
cout<< num;
cout<< endl;
cout<< "Please follow the directions!";
cout<< endl;
}
cout<< "you selected";
cout<< num;
cout<< endl;
return 0;
}
 
even though i specified it too only when the number in an invalid choice

I don't see that in your code.
What your code does is get a number from the user and then print a newline, followed by "Please enter a number from 1 to 10: ", followed by the number the user entered, followed by another newline, followed by "Please follow the directions!", followed by another newline.
Then it goes back to the top of the loop and checks if the number is less than 1 or bigger than 10 (and if it is, it repeats all of that).
would adding an else statement in at the be way to correct it?
closed account (iAk3T05o)
The position of things cause that.
1
2
cout << "Please enter a number, 1 to 10: ";
num = ask();

Try that.
Topic archived. No new replies allowed.