Thread: C tutorial Switch..case loop

  1. #1
    Registered User
    Join Date
    Apr 2010
    Posts
    51

    C tutorial Switch..case loop

    I am currently working my way through the C tutorials and have a question about adding a loop to the switch..case tutorial code.
    I have tried to add a loop so that if the selection is less than or equal to 4, the switch case is offered, else- default option prints error message.
    Will this loop back to the top of the program and run it again?

    Anyway I get an error "expected expression before 'else'", am I close with my code or not?

    Code:
     
    #include <stdio.h>
    
    
    int playgame();
    int loadgame();
    int multiplayer();
    
    int main()
    {
    	int input;
    	printf("1. Playgame\n");
    	printf("2. Loadgame\n");
    	printf("3. Multiplayer\n");
    	printf("4. Exit\n");
    	printf("Selection: ");
    	scanf ("%d", &input);
    	if (input <= 4){
    	switch (input){
    		case 1:
    		playgame();
    		break;
    		case 2:
    		loadgame();
    		break;
    		case 3:
    		multiplayer();
    		break;
    		case 4:
    		printf("Thank you for playing\n");
    		break;
    	}
    		else{
    		default:
    		printf ("Bad input, make another selection: \n");
    			scanf("%d", &input)
    		break;
    	}
    	}
    	getchar();
    }
    
    int playgame(){
    	printf("you are playing\n");
    	return 0;
    }
    
    int loadgame(){
    	printf("game is loading\n");
    }
    
    int multiplayer(){
    	printf("Find a friend to play with\n");
    }

  2. #2
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    The 'default' switch statement is just like a switch case, except it catches all the cases that haven't been caught by cases you specify. The default case goes inside the switch just like the other cases.

    Also note that you haven't closed off your if statement before you have written your else statement.

    You haven't actually put in a loop so it won't go back to the beginning.

  3. #3
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Thanks DP

    I took out the 'if' as I believe it did nothing, and the "try again" scanf under default, as it didnt go back to the top of the main.
    The instructions are "put a loop around the whole thing to wait for valid input"
    To make the program loop in case of a bad input, any hints? (hints are fine, dont need the code , justa push in the right direction please)

  4. #4
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You want to loop at least once and only want to exit if 'input' is 4.

    Sounds like a good place for a do...while loop.

  5. #5
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Good advice, I understand the theory, but have trouble with the code.
    Only problem is that now the printf statements in the functions repeat non-stop. The only way to stop it is to close the command window.
    If I put the getchar(); above the while, it performs the function twice, then repeats when return is pressed

    Also in the functions, if I dont use "return 0;" I get a warning 'control reaches end of non-void function', but this doesn't seem to affect the way the program runs.

    Code:
    #include <stdio.h>
    
    
    int playgame();
    int loadgame();
    int multiplayer();
    
    int main()
    {
    	int input;
    	printf("1. Playgame\n");
    	printf("2. Loadgame\n");
    	printf("3. Multiplayer\n");
    	printf("4. Exit\n");
    	printf("Selection: ");
    	scanf ("%d", &input);
    do {
    	switch (input){
    		case 1:
    			playgame();
    			break;
    		case 2:
    			loadgame();
    			break;
    		case 3:
    			multiplayer();
    			break;
    		case 4:
    			printf("Thank you for playing\n");
    			break;
    		default:
    			printf ("Bad input \n");				
    			break;	
    			
    	}
    	
    } while (input != 4);
    			
    	getchar();
    }
    
    
    int playgame(){
    	printf("you are playing\n");
    	
    }
    
    int loadgame(){
    	printf("game is loading\n");
    	return 0;
    }
    
    int multiplayer(){
    	printf("Find a friend to play with\n");
    	return 0;
    }
    Last edited by dunsta; 04-18-2010 at 04:26 AM.

  6. #6
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    oh, ok. I had the "do{" in the wrong place. It works much better just after the input variable is initiated, before the first printf for the menu.
    Thank you

  7. #7
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    Oh, I thought you were planning for the user to choose what they wanted to do every time.

    If you only want it to loop in case of 'bad input', then you're probably better off setting a flag variable when the input is unrecognised and then checking that.

    And the type before a function name specifies the return type of the function. If you declare a function as returning int then you must return an int. If you don't wish to return anything, declare your function as 'void'.

    EDIT: Oh ok, all good then.

  8. #8
    Registered User
    Join Date
    Apr 2010
    Posts
    51
    Thanks DeadPlanet

  9. #9
    Registered User
    Join Date
    Jan 2009
    Location
    Australia
    Posts
    375
    You're welcome

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Checking array for string
    By Ayreon in forum C Programming
    Replies: 87
    Last Post: 03-09-2009, 03:25 PM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 09:35 AM
  3. Format Precision & Scale
    By mattnewtoc in forum C Programming
    Replies: 1
    Last Post: 09-16-2008, 10:34 AM
  4. get keyboard and mouse events
    By ratte in forum Linux Programming
    Replies: 10
    Last Post: 11-17-2007, 05:42 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM