1. ## faulty while loop

I swear this is my last one for the night. This while loop should be presenting the menu until a valid option is selected. Right now it just repeats the menu even after a valid option is selected. I'm guessing my logic is messed up in my evaluation statement.

Code:
```int displayMenu (){

int x=0;

do{
printf("\n1.\tThis is option 1");
printf("\n2.\tThis is option 2");
printf("\n3.\tThis is option 3");
printf("\n4.\tThis is option 4");
printf("\n5.\tThis is option 5");
printf("\n9.\tThis is option 9");

scanf("%d",&x);

if (x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)
}while(x<1 || x >5 && x<9 || x>9);

return x;
}```

2. ## Re: faulty while loop

Originally posted by monkey_C
I swear this is my last one for the night. This while loop should be presenting the menu until a valid option is selected. Right now it just repeats the menu even after a valid option is selected. I'm guessing my logic is messed up in my evaluation statement.

Code:
```int displayMenu (){

int x=0;

do{
printf("\n1.\tThis is option 1");
printf("\n2.\tThis is option 2");
printf("\n3.\tThis is option 3");
printf("\n4.\tThis is option 4");
printf("\n5.\tThis is option 5");
printf("\n9.\tThis is option 9");

scanf("%d",&x);

if (x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)
}while(x<1 || x >5 && x<9 || x>9);

return x;
}```
I don't even understand what you are trying to accomplish. What does the user input to quit? Try putting some paranthesis around your statements too like this.

while( ((x < 1) || (x > 5)) && ((x < 9) || (x > 9)) );

Something like that. Post back with some more specifics.

3. ## Re: Re: faulty while loop

Originally posted by MrWizard
I don't even understand what you are trying to accomplish. What does the user input to quit? Try putting some paranthesis around your statements too like this.

while( ((x < 1) || (x > 5)) && ((x < 9) || (x > 9)) );

Something like that. Post back with some more specifics.

This function displays a menu to the user then prompts for input, validating the input information and repeating the menu until the info IS valid. Option 9 will eventually be used to exit the program, but it should be returned like all other options. Instead if you hit 9 it will return it back to main, but any other option will leave you in this menu. Any explanations?

***note, the parentheses, while helping to clarify, did nothing else for my program.***

4. This would be a good time to use switch(), I'd recommend it

Code:
```printf("\n1.\tThis is option 1");
printf("\n2.\tThis is option 2");
printf("\n3.\tThis is option 3");
printf("\n4.\tThis is option 4");
printf("\n5.\tThis is option 5");
printf("\n9.\tThis is option 9");

scanf("%d",&x);

switch(x)
{
case 1:/* do something*/
case 2:/* do something*/
case 3:/* do something*/
case 4:/* do something*/
case 5:/* do something*/
case 9:/* do something*/
default: printf("Not a valid option");
}```
Make sense?

5. Well, if you want the number 9 to exit the loop try this

}while( x != 9 );

That will continue looping until 9 is pressed. I think that is what you are asking? Maybe something like this...

Are you trying to loop on invalid input? (i.e. x < 1 and x > 5? ) If you want the loop to continue on invalid choice then try this...

}while( (((x < 1) || (x > 5))) && ( x != 9 ) )

6. My solution to things like this is usually to put it all in a for(;;), and break if I find something valid. Like:

Code:
```for(;;)
{
fgets(string, 51, stdin);

if(strcmp(string, "stop\n") == 0) break;

}```
which just echos the string until someone types in stop.

There might be some performance issues involved that I don't know about, but I've been using this for a while for presenting menus, and I haven't had any problems with it.

Hope this helps.

starX
www.axisoftime.com

7. If you're doing this:
>scanf("%d",&x);
...always check the return code to ensure it did what you asked.

8. Originally posted by starX
My solution to things like this is usually to put it all in a for(;;), and break if I find something valid. Like:

Code:
```for(;;)
{
fgets(string, 51, stdin);

if(strcmp(string, "stop\n") == 0) break;

}```
which just echos the string until someone types in stop.

There might be some performance issues involved that I don't know about, but I've been using this for a while for presenting menus, and I haven't had any problems with it.

Hope this helps.

starX
www.axisoftime.com
Its just an opinion well i belive that break can be quite a bad habit .Isntead of using break u can use a while structure that does the job for you .... E.g like the goto statemant when new cprogrammers start to apply it in there programms to make stuff easier they make a whole lot of mess about it ....

9. When this condition

(x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)

is true, then an invalid option is choosen and the loop repeats. In other words when this condition

!(x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)

is true, then a valid option is choosen and the loop can end.

I agree with datainjector that the use of break in loops is not very elegant programming. You're using a for-loop, the for-statement has a section in which a condition is checked. It would be more elegant to use this section. Or even better, since this kind of loop has nothing to do with for, would be to use while.
[/edit]