Thread: C: Default case in switch not working

  1. #1
    Registered User LunaLux's Avatar
    Join Date
    Apr 2011
    Location
    Malaysia for the time being.
    Posts
    13

    Unhappy C: Default case in switch not working

    Hey all!

    I don't mind poring for hours over code and trying to learn something by doing things in different ways but this is just PAINFUL. I cannot figure out why the default statement will not show up. If anyone could point me in the right direction, I would greatly appreciate it.

    This is an extremely simplified version of my original code. The yes/no option worked at first after entering in A, B, or C, then the .exe suddenly started... ending itself. Or Windows did, rather. >_>

    Code:
    #include <stdio.h>
    
    main()
    { 
    char code, contexit, name;
    
    do
    {
        printf("\nEnter name: ");
        scanf("%s", &name);
        
        do 
        {
            printf("\nEnter code A/B/C: ");
            scanf(" %c", &code);
        }
        while(code!='A' && code!='B' && code!='C');
    
        switch (code)
         {
            case 'A':
                  printf("\nAAA: ");
                break;
            case 'B':
                 printf("\nBBB \n");
                break;
            case 'C':
                printf("\nCCC \n");
                break;
             default:
                printf("Please try again.");
                break;    
        }
        
        printf("\nYour name is: %s", name);
        printf("\n\nContinue? Y/N\n");
        scanf(" %c", &contexit);
    }
        while(contexit=='Y');
    
    }
    Thanks in advance!
    Last edited by LunaLux; 04-23-2011 at 01:29 PM.

  2. #2
    Registered User
    Join Date
    Apr 2011
    Location
    Las Vegas
    Posts
    66
    You have a couple of problems here - the first one is that main should be defined to return an int, and you should return an int value at the end of the function. The second one is more severe -- you are trying to read a C string and store it in a char character. Fix these and your program should work better...

    The reason your default case never executes is because the ONLY possible values of code are A, B, and C.

    Kevin

  3. #3
    Registered User LunaLux's Avatar
    Join Date
    Apr 2011
    Location
    Malaysia for the time being.
    Posts
    13
    Thanks for the prompt reply! I shall try that now and see how it goes.
    As a newbie, this forum seems awesome, I hope to be sticking around.

    - Tabs.

  4. #4
    Registered User
    Join Date
    Apr 2011
    Posts
    1
    kmess is right, the default cannot be seen because of these

    Code:
     do 
        {
            printf("\nEnter code A/B/C: ");
            scanf(" %c", &code);
        }
        while(code!='A' && code!='B' && code!='C');
    this do while statement prevents the input from other input other than A,B and C
    because it will loop and loop until it gets the A,B or C

  5. #5
    Registered User LunaLux's Avatar
    Join Date
    Apr 2011
    Location
    Malaysia for the time being.
    Posts
    13
    Okay.. Basically someone suggested something about input validation loops after I told him what Kevin posted, and found something which led me to put:

    Code:
            x=0;
    	while(x==0) 
    	{
    		printf("\nEnter code (A/B/C):) ");
    		scanf(" %c", &code);
    		if (code=='A'||code=='B'||code=='C')  
    			x=1;
    		else  
    			printf("\nInvalid data entry. Please enter a single letter (A/B/C): \n");
    	}
    Right after the code was my switch, worked with no problems.

    I've got one more question: is a sentinel value (via user input) a good idea here at all? I know what they are and where they're put in.... But in this program there are many loops. If I put in a value to end the loop it just moves on to the next one... Is there a way for the user to type in a value at any time that will just terminate the entire program completely? I've got both numeric and alpha input in different consecutive input validation loops, so I can't use a single value to terminate everything... Can I?

    (I have a feeling my lecturer did not explain sentinel-controlled loops clearly and so I'm trying to figure them out on my own.)

    Also, with the int and return 0, some places say this is not necessary but standard procedure to check the program is working? XD I put it in anyway, it hadn't made a difference.

    Thanks though, definitely put me on the right track.

    - Tabs
    Last edited by LunaLux; 04-24-2011 at 08:59 AM.

  6. #6
    Banned
    Join Date
    Aug 2010
    Location
    Ontario Canada
    Posts
    9,547
    I would have fixed your code this way....
    In particular, you were not allocating array space for the name.

    Code:
    #include <stdio.h>
    
    int main(void)  
    { 
    char code, contexit, 
    char name[21];
    
    do
    {
        printf("\nEnter name: ");
        scanf("%20s", &name);
        
        printf("\nEnter code A/B/C (X to quit) : ");
        scanf(" %c", &code);
        switch (toupper(code))
           {
                case 'A':
                    printf("\nAAA: ");
                    break;
                case 'B':
                    printf("\nBBB \n");
                    break;
                case 'C':
                    printf("\nCCC \n");
                    break;
                case 'X';
                    Printf("Bye for now %s...\n\n",name);
                    exit(0); 
                default:
                    printf("Please try again.");
                break;    
             }    
         printf("Your name is : %s",name); 
       }
    
        return 0; }
    I would have done that so that as your program expands and changes you can replace the printf(\n"AAA\n"); (etc) lines with function calls that could make the program do various things for you... for example typing B might ask the user for their address or A might add two numbers ... or whatever your imagination can conjure.
    Last edited by CommonTater; 04-24-2011 at 08:48 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. switch () {default: <something>; case ...
    By Kennedy in forum C Programming
    Replies: 4
    Last Post: 11-30-2010, 12:40 PM
  2. Switch statement / default:
    By kcpilot in forum C Programming
    Replies: 4
    Last Post: 12-02-2008, 03:14 PM
  3. case switch not working
    By AmbliKai in forum C Programming
    Replies: 2
    Last Post: 10-09-2008, 06:42 AM
  4. Replies: 11
    Last Post: 08-25-2008, 12:01 PM
  5. default switch statement not working
    By gL_nEwB in forum C++ Programming
    Replies: 3
    Last Post: 05-13-2006, 10:13 AM

Tags for this Thread