faulty while loop

This is a discussion on faulty while loop within the C Programming forums, part of the General Programming Boards category; I swear this is my last one for the night. This while loop should be presenting the menu until a ...

  1. #1
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    Unhappy 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");
    			
    			printf("\n\n\nPlease enter a menu option:");
    			scanf("%d",&x);
    			
    	
    			
    			if (x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)
    				printf("\a\a\a\n\n\n\n\n\nI'm sorry please pick a valid menu option!!!\n\n\n");
    		}while(x<1 || x >5 && x<9 || x>9);
    		
    		
    return x;
    }
    don't be a two-bit user

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571

    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");
    			
    			printf("\n\n\nPlease enter a menu option:");
    			scanf("%d",&x);
    			
    	
    			
    			if (x!=1 && x!=2 && x!=3 && x!=4 && x!=5 && x!=9)
    				printf("\a\a\a\n\n\n\n\n\nI'm sorry please pick a valid menu option!!!\n\n\n");
    		}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. #3
    Registered User
    Join Date
    Sep 2002
    Posts
    16

    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. #4
    Registered User Inept Pig's Avatar
    Join Date
    Apr 2002
    Posts
    140
    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");
    			
    printf("\n\n\nPlease enter a menu option:");
    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?
    Money frees you from doing things you dislike. Since I dislike doing nearly everything, money is handy - Groucho Marx

  5. #5
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    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. #6
    Registered User
    Join Date
    Aug 2001
    Posts
    202
    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;
    
      printf("Your string is: %s", string);
    }
    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
    Last edited by starX; 09-13-2002 at 09:03 AM.

  7. #7
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    If you're doing this:
    >scanf("%d",&x);
    ...always check the return code to ensure it did what you asked.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  8. #8
    Registered User datainjector's Avatar
    Join Date
    Mar 2002
    Posts
    356
    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;
    
      printf("Your string is: %s", string);
    }
    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 ....
    "I wish i could wish my wishs away"

    "By indirections find directions out" -- William Shakespears

    "Do what thou wilt shall be the whole of the law" -- Crowley "THE BEAST 666"

    Mizra -> love = Death...
    RDB(Rocks yooo)..

    http://www.cbeginnersunited.com

    Are you ready for the Trix ???

  9. #9
    ....
    Join Date
    Aug 2001
    Location
    Groningen (NL)
    Posts
    2,386
    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.

    [edit]
    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]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. My loop within loop won't work
    By Ayreon in forum C Programming
    Replies: 3
    Last Post: 03-18-2009, 11:44 AM
  2. While loop misbehaving (or misunderstanding)
    By mattAU in forum C Programming
    Replies: 2
    Last Post: 08-28-2006, 03:14 AM
  3. Personal Program that is making me go wtf?
    By Submeg in forum C Programming
    Replies: 20
    Last Post: 06-27-2006, 01:13 AM
  4. loop issues
    By kristy in forum C Programming
    Replies: 3
    Last Post: 03-05-2005, 09:14 AM
  5. when a while loop will stop ?
    By blue_gene in forum C Programming
    Replies: 13
    Last Post: 04-20-2004, 04:45 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21