Thread: Switch Statement Question

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    7

    Question Switch Statement Question

    Code:
    #include <stdio.h>
    
    int main()
    {
        //Hold Numeric Char entered by user
        char c;
    
        //holds boolean value for Done
        int done;
    
        //Holds the value of the total price
        float total = 0;
    
        //Prompt
        printf("Hello, please enter your selection: \n");
        printf("1 - Candy\n");
        printf("2 - Beverage\n");
        printf("3 - Hotdog\n");
        printf("4 - Popcorn\n");
        printf("0 - Done\n");
        printf("Your Choices: \n");
    
        //Variable done Initialized to 0(False)
        done = 0;
        
        //Get and Handle Input
        while(!done)
        {
            //Get Input
            c = getchar();
    
            switch(c)
            {
                case '1':
                    printf("Candy\t$3.00\n");
                    total+=3;
                    break;
                case '2':
                    printf("Beverage\t$5.50\n");
                    total+=5.5;
                    break;
                case '3':
                    printf("Hotdog\t$10.00\n");
                    total+=10;
                    break;
                case '4':
                    printf("Popcorn\t$4.99\n");
                    total+=4.99;
                    break;
                case '0':
                    printf("Total: $%.2f\n", total);
                    printf("Please Pay at the Cashier, Thank You.");
                    done = 1;
                    break;
                default:
                    printf("Improper Selection!");
                    break;
            }
        }
    
        getchar();
        return 0;
    }
    I'm Using break; on every case, but whenever i type in any number,
    default is always executed despite the break statements...
    Why is that? :S

  2. #2
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    >c = getchar();
    Some character(s) remain in the input buffer, to be getchar 'ed in the next run.
    Replace it with:
    > scanf(" %c",&c);

    Also, your loop control is ..somewhat suspicious ..though nothing is wrong with it if you wanted exactly that...
    Last edited by manasij7479; 08-23-2011 at 03:29 AM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    Because getchar() is called at least twice when you enter say
    1\n

    The 1 does what you want
    The \n goes to the default.

    You could have a
    case '\n': break;

    just to ignore the newline you always expect, but never care about.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  4. #4
    Registered User
    Join Date
    Aug 2011
    Posts
    7
    Quote Originally Posted by Salem View Post
    Because getchar() is called at least twice when you enter say
    1\n

    The 1 does what you want
    The \n goes to the default.

    You could have a
    case '\n': break;

    just to ignore the newline you always expect, but never care about.
    ´

    Thank You, i changed getchar() to scanf() and created a new switch case for \n
    and it worked like it should.

    I don't really understand why \n goes to the default though :S

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Jony View Post
    ´

    Thank You, i changed getchar() to scanf() and created a new switch case for \n
    and it worked like it should.

    I don't really understand why \n goes to the default though :S
    You didn't need to do both(if you put scanf ...don't miss the space before % in this case) !

    Anyway.. The newline remains and possibly goes through default because it represents the Enter you have hit after putting the character.

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,661
    > I don't really understand why \n goes to the default though :S
    Because the default case is what matches EVERYTHING else apart from the 0 to 4 you've already identified.
    \n is just another character as far as getchar() is concerned.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question about the "switch" statement
    By SensualCake in forum C# Programming
    Replies: 2
    Last Post: 11-29-2010, 08:44 PM
  2. Question about break in switch statement
    By alone2002dj in forum C Programming
    Replies: 1
    Last Post: 12-09-2007, 08:42 PM
  3. Using Switch statement
    By Ubha in forum C Programming
    Replies: 8
    Last Post: 12-24-2006, 12:34 AM
  4. After the switch statement?
    By cerin in forum C++ Programming
    Replies: 8
    Last Post: 03-02-2005, 08:01 PM