Switch/Case Fallthrough BUG

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • DaemonCoder
    New Member
    • Mar 2008
    • 43

    Switch/Case Fallthrough BUG

    I have a switch/case statement using numerics for a menu, but when i enter an alphanumeric statment it goes into a loop printing my menu and the default statement from switch/case the default statement. The only way to end it is ctrl-c. the code is below. Can someone please explain this quirk to me?

    [CODE=c]
    #include<stdio. h>
    #include<stdlib .h>
    int CHOICE;
    int LTENC[11];

    //SNIPPED - cArrayModify(); / pLastTen();

    void doNothing()
    {
    printf("\n");
    cArrayModify();
    }

    void invalidChoice()
    {
    printf(" Invalid Choice!! \n");
    cArrayModify();
    }

    void menu()
    {
    cArrayModify();
    printf(" \n \n");
    printf(" Menu: \n");
    printf(" 1 - Print Menu \n");
    printf(" 2 - Do nothing \n");
    printf(" 3 - Exit \n \n");
    pLastTen();

    }

    int programexit()
    {
    printf("\n Exit Conditions met, program will now exit!\n");
    return 0;
    }

    void commandlp()
    {
    while(1)
    {
    printf("-> ");
    scanf("%d",&CHO ICE);
    switch(CHOICE)
    {
    case 1 : menu();
    break;

    case 2 : doNothing();
    break;

    case 3 : programexit();
    exit(0);

    default: invalidChoice() ;
    break;

    }
    }

    }


    int main()
    {

    menu();
    commandlp();

    }[/CODE]
  • oler1s
    Recognized Expert Contributor
    • Aug 2007
    • 671

    #2
    Please give an example. Show the program input you used, a bit of sample output, etc.

    Comment

    • gpraghuram
      Recognized Expert Top Contributor
      • Mar 2007
      • 1275

      #3
      You are only coming out of switch statement and not the while loop.
      The break u have given is only for switch statement.
      So you should also exit from while loop or change while to if

      Raghuram

      Comment

      • DaemonCoder
        New Member
        • Mar 2008
        • 43

        #4
        Code:
         Menu:
         1 - Print Menu
         2 - Do nothing
         3 - Exit
        
        
         Last ten commands: 0, 0, 0, 0, 1, 1, 1, 1, 5,-> s
        
        ->  Invalid Choice!!
        ->  Invalid Choice!!
        ->  Invalid Choice!!
        ->  Invalid Choice!!
        ->  Invalid Choice!!   //  this loops to this with no further user input...
        
        -> ^C
        The reason i didnt break oout of the while loop is because this is a menu with an exit function. I dont want it to exit unless the user says to exit.

        Comment

        • oler1s
          Recognized Expert Contributor
          • Aug 2007
          • 671

          #5
          Yeah, there's a bug in your code. And it's not the switch statement. I was going to type up a large post, but I realized that an article on scanf explains the issue.

          Comment

          • DaemonCoder
            New Member
            • Mar 2008
            • 43

            #6
            So using scanf() is my problem... So now i have another issue...

            [CODE=C]

            <SNIPPED>
            while(1)
            {
            printf("-> ");
            sscanf("%*s",CH OICE_STRING);

            CHOICE = strcmp (CHOICE_STRING, commandListVari able);
            // if commandListVari able == "exit" and CHOICE_STRING == "exit"
            // what does this return...???

            switch(CHOICE)
            {

            <snipped>

            [/CODE]
            Do i have to create a seperate variable for each command or is there a way to use strcmp for a direct comparison to a literal so that i get the correct command executed in the switch statement??

            The information that i found says "Returns an integral value indicating the relationship between the strings". How do i 'predict' what the valuse of the comparison will be?

            Comment

            Working...