While loop difficulties

Task-

Ask the user to enter a number between 5 and 8 (inclusive).

Use a while loop to validate the choice. It should continue until the user puts the correct number

I am stuck here

My soln so far-

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include <iostream>
#include <cstdio>
using namespace std;
int main(){
    
int input;    

cout<<"Enter a number between 4 and 9:\n";  
cin>>input;  
while (5>input||input>8)
      cout<<"Invalid number, please try again:\n";
      

}

This throws me into an infinite loop. I can't seem to understand the problem. Help!
Last edited on
You just failed to add the "{" at start of the while.

So, your code should be:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
   int input;    

   cout<<"Enter a number between 4 and 9:\n";  
   cin>>input;  

   while (5>input||input>8)
   {
         cout<<"Invalid number, please try again:\n";
   }
}


EDIT: Sorry. My fault. Didn't see that the "}" was from main, not from while.
Last edited on
Still getting same problem even when i incorperate "{"
Could you post the entire code ? (if this is part of a larger cpp file)
This is it ;x I'm new to this. sorry
What program did you use to compile this code ?
dev c++
Well yes. It's kinda' infinite loop. You just ask for ONE number. The "while" statement runs for the number you entered. But after that, it won't stop unless you use a "break;" or a "goto"
Do you want the program to ask the user if he wants to try entering another number when the number is invalid ?

If yes, you must use a "goto" or maybe a more simple way. If not, you got to use "break;".
Last edited on
Yes I want the user to keep inputing a number until he gets it right. I am familiar with break to end the loop, but not with goto?
Here is the code:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
#include <iostream>
#include <cstdio>

using namespace std;

int main()
{
   int input;
   char ok;    

   cout<<"Enter a number between 4 and 9:\n";  
   cin>>input;  

   while (5>input||input>8)
   {
      cout<<"Invalid number, please try again:\n";
      cout << "Try with other number ("y" = yes / "n" = no) ?\n";
      cin >> ok;
      if (ok == 'y');
          cin >> input;
      else
      {
          if (ok == 'n')
              cout << "You have chosen to exit.";
          break;
      }
   }

   return 0;
}
Last edited on
Hope I could help :)
And about the goto... If you are new, you can just learn about it later. For the moment you got to understand simple codes.
What if I didn't want it to be an option to select another number, just wanted the program to automatically prompt the use for another number. is that possible? ._.
Then remove the both cout statements and just have the cin >> input. Then remove the if else block and the break.

So basically...

while (5>input||input>8)
{
cin >> input;
}

//What you want to do after the correct number entered..

Topic archived. No new replies allowed.