Problem with for loop

When asked to enter a number between 1-3 it should give an error and reask the user to enter another number but when i enter 4 it accepts it.
Here is my code:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
cout<<"Player Options: 1=Build House/Hotel, 2= Trade, 3=Mortgage "<<endl;
                       cin>>options;
                   for(int z=0;z<2;z++)
    {
    if(options<1)
    {
                 cout<<"invalid input Eneter a number 1-3"<<endl;
                 cin>>options;
                 z=0;
                 } 
                 else if(menu>3)
                 {
                      cout<<"invalid input Eneter a number 1-3"<<endl;
                 cin>>options;
                 z=0;
                  } 
                  }
}


Any help?
What is up with that crazy indentation?
Well anyway, you have a rather... silly means of restricting your input. I don't know what it is and somebody will probably bring up something obvious, but there's a better way to do it.
1
2
3
4
5
6
7
8
cout << "Let's have a number from 1 to 10 ";
cin >> thenum;

while (thenum < 1 || thenum > 10)
{
    cout << "That's not a valid number. ";
    cin >> thenum;
}

If you don't know yet, while is a conditional loop. It's, unlike for, designed to run as many times as it has to, and it could be indeterminate. The condition, if you don't recognize the || operator, basically means
"thenum less than one OR thenum greater than 10"
You can check the tutorial on this website for a full introduction, but that's the kind of code I use to check my input for validation. Try something like that and see what happens.
thx im taking a programing class and were not that far into it but i shall adapt this shorter method of doing it haha
Why dont you use switch case for option .
Topic archived. No new replies allowed.