Thread: how to use case/switch with letters and numbers

  1. #1
    Registered User
    Join Date
    Feb 2009
    Posts
    11

    how to use case/switch with letters and numbers

    Hey

    I am trying to use a case switch statement to choose from a menu option but It will not allow to use letters as an option. If the user put in a letter I would like for it to say invalid option but instead it continuously loops.

    Code:
    #include <stdio.h>
    
    void playgame();
    void loadgame();
    void multi();
    void scores();
    void quit();
    
    int main (int argc, const char * argv[]) {
        // insert code here...
       
    	
    	char input;
    		
    	
    	do{
    		
    		printf("\n\n");
    		printf("1. Play New Game\n");
    		printf("2. Load Previous Game\n");
    		printf("3. Play Multiplayer\n");
    		printf("4. View High Scores\n");
    		printf("5. Quit\n");
    		
    		
    		
    		
    	
    	printf("\nChoose an option and press enter:   ");
    	
    	
    	scanf("%d",&input);
    	
    	
    	
    	
    	switch (input) {
    		case 1:
    			playgame();
    			break;
    		case 2:
    			loadgame();
    			break;
    		case 3:
    			multi();
    			break;
    		case 4:
    			scores();
    			break;
    		case 5:
    			quit();
    			break;
    		
    		default:
    			printf("\nInvalid Choice!\n");
    			break;
    	}
    		
    	} while (input !=5);
    	
    
    	
    	
    	//end
        return 0;
    }
    
    
    void playgame(){
    printf("\nStarting new game....");
    }
    
    
    void loadgame(){
    printf("\nChoose from a save file");
    }
    
    void multi(){
    printf("\nConnecting to servers!");
    }
    
    void scores(){
    printf("\nHigh Scores~");
    }
    
    void quit(){
    	printf("\nThanks for playing...");
    
    	
    }
    Any help would be fantastic.

  2. #2
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    you need to clear the stdio from the jusk left there by scanf before calling scanf again

    search FAQ or/and forum how to flush stdin (using fflush is BAD idea)
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  3. #3
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Note that break exits out of the immediately enclosing while, do, for or switch statement.

  4. #4
    Registered User
    Join Date
    Jun 2005
    Posts
    6,815
    Quote Originally Posted by vart View Post
    you need to clear the stdio from the jusk left there by scanf before calling scanf again
    No need to clear the stream. Just need to respond depending on type of input.

    If an error occurs with scanf() (eg %d encounters character data that does not correspond to an integer) then the last character read is left in the stream, and will be read again on the next call. Hence the looping.

    One option is to use the fact that scanf() returns the number of fields successfully read. So, given the %d format specifier, scanf() will return 1 if an integer is read and 0 otherwise. If it returns zero, read a single character from the stream and try again.

    Alternatively, read the data as a string (eg using fgets()). Then interpret the string to see if it contains an integer or other character data (eg using sscanf()). If it contains an integer, act accordingly. Otherwise, if it contains some other character, respond as needed.

    One point to remember: it is not a good idea to mix up method for reading a stream. If you use scanf()/fscanf() don't use the fgets() approach and vice versa. Each approach makes assumptions that the other violates.
    Right 98% of the time, and don't care about the other 3%.

    If I seem grumpy or unhelpful in reply to you, or tell you you need to demonstrate more effort before you can expect help, it is likely you deserve it. Suck it up, Buttercup, and read this, this, and this before posting again.

  5. #5
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    Well here is an solution, in addition to grumpy solutions

    Code:
        ch = getchar();
        
        if( ch >= '0' && ch <= '5' )
            printf("Numbers!!\n");
        else
            printf("Invalid input\n");
    But note this will fail when you try to enter more than one char. Like for example 'aa', '10' etc

    And ch should be a declared as an int

    -ssharish
    Life is like riding a bicycle. To keep your balance you must keep moving - Einstein

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 04-19-2005, 08:05 PM
  2. Numbers to Letters
    By flip114 in forum C Programming
    Replies: 5
    Last Post: 09-24-2003, 03:18 AM
  3. converting numbers to letters??
    By Lau in forum C Programming
    Replies: 5
    Last Post: 11-06-2002, 12:17 PM
  4. Convert Phone Letters To Numbers
    By nicolobj in forum C Programming
    Replies: 4
    Last Post: 10-03-2002, 03:53 PM
  5. Transforming letters to numbers...
    By N8760 in forum C++ Programming
    Replies: 2
    Last Post: 12-23-2001, 03:26 PM