Problem with loop inside switch statement

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • geebanga88
    New Member
    • Aug 2007
    • 62

    #1

    Problem with loop inside switch statement

    Hi, i have a menu program, when i select option C and after going back to the menu, if i enter a vaild option it will say its invalid. However if i select option a or b and then enter an invalid option and then enter a valid option it wont say the valid option is invalid. ?

    Code:
     
    char menu_option; 
        int count, number;
        	
            do
    	{ 
            
            printf ("\n\nYou have four choices.\n");
    		printf ("Enter a to print your name\n");
    		printf ("Enter b to print your tutorial time\n");
    		printf ("Enter c to input a number between 1 and 50\n");
    		printf ("Enter q to quit.\n"); 
    		
    		scanf ("%c%*c", &menu_option); 
    		menu_option = tolower(menu_option);
    		
    			switch( menu_option )
    		{
    			case 'a':
    				printf("You have selected option A\n\n");
    			 	break;
    			
                case 'b':
    				printf("You have selected option B\n\n");
    			 	break;
    			
                case 'c':
    				printf("You have selected option C\n"); 
    				printf("Please enter a number between 1 and 50:\n"); 
    				scanf("%i", &number); 
                    
                    if ( number < 1 || number > 50) 
                       printf("Invalid number\n\n"); 
                    else 
                         for (count = 0; count <= number; ++count){
                             printf("%i,", count);     
                             }
                    break;
    			
                case 'q':
    				break;
    			
                default:
    				printf("You have not selected a valid option\n\n");
    		} 
    		
    	} while (menu_option != 'q');
  • dfound
    New Member
    • Jan 2007
    • 52

    #2
    try this...im not sure whether it will work but it worked for me...




    Code:
        ......................
    
    menu_option=getch(); // u can also try cin>>menu_option; , 
    
                ..................................
    
    
    case 'c': {
    
               .......... // ur code
            
               } 
               break;
    
    case 'q':

    I think this will help..

    Comment

    • geebanga88
      New Member
      • Aug 2007
      • 62

      #3
      Sorry im not sure what getch(); is?, and do u need a header file to declare it?. And isnt cin>> a c++ function, im only working in C.

      Comment

      • sicarie
        Recognized Expert Specialist
        • Nov 2006
        • 4677

        #4
        Originally posted by geebanga88
        Sorry im not sure what getch(); is?, and do u need a header file to declare it?. And isnt cin>> a c++ function, im only working in C.
        I would recommend scanf() if you are working in C. Getch() is a method from conio.h, but as conio is a non-standard header (depending on the version, you may get a different implementation, which can cause problems with your program), I would stick with scanf() or another of the C input functions.

        Comment

        Working...