Thread: skipping switch statement

  1. #1
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28

    skipping switch statement

    Greetings,

    I am having a problem with a function skipping over my switch statement and going right to the default. I have a getchar() statement but it rolls right through. Can anyone tell me why? Here is the function
    Code:
    char choice_selection;      
    choice_selection = getchar();  
          
     
       
       switch(choice_selection)
               {
               case ('A'):
                     {
               
                     int c_choice;
                     char c_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(c_Col_line, sizeof(c_Col_line), stdin);
                     sscanf(c_Col_line, "%d", &c_choice);
                     
                     if(c_choice*c_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     //goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (c_choice*c_price));
                     *my_c_units_ptr = c_choice;
                     }
                     }
                     
       
       
               case ('B'):
                     {
                     int h_choice;
                     char h_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(h_Col_line, sizeof(h_Col_line), stdin);
                     sscanf(h_Col_line, "%d", &h_choice);
                     
                     if(h_choice*h_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     //goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (h_choice * h_price));
                     *my_h_units_ptr = h_choice;
                     }
                     }
                case ('C'):
                     //goto start_buy;
       
       
               case ('D'):
                     //goto start_buy;
    
    
    
    
    
    
    
    
               case ('E'): 
                     //goto start_buy;
    
    
    
    
    
    
    
    
               case ('F'): 
                     //goto start_buy;   
    
    
    
    
               case ('G'): 
                     //goto start_buy;    
       
               default :
                       //goto start_buy;
                 printf("Default\n");
      /* try again */
       }

  2. #2
    Registered User
    Join Date
    Jul 2012
    Posts
    51
    You forgot to add:
    Code:
    break;
    Example:
    Code:
    #include <stdio.h>
    
    int main ()
    {
        int x = 'b';
        switch (x)
        {
            case 'a':
                printf ("a\n");
                break;
            case 'b':
                printf ("b\n");
                break;
            case 'c':
                printf ("c\n");
                break;
            default:
                printf ("Please try again.\n");
        }
        return 0;
    }
    There may be other problems?

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Please post the entire function.

  4. #4
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    Code:
    void The_buy(                   double *my_money_ptr, 
                       int *my_c_units_ptr, 
                       int *my_h_units_ptr, 
                       int *my_r_units_ptr, 
                       int *my_p_units_ptr, 
                       int *my_a_units_ptr, 
                       int *my_p_units_ptr )
    {
              srand(time(NULL));
              double c_price = 30000 *rand() % 1;
              double h_price = 5000 * rand() % 1;
              double r_price = 1000 *rand() % 1;
              double p_price= 800 *rand() % 1;
              double a_price = 500 *rand() % 1;
              double p_price =100 *rand() % 1;
            
              
    
    
          
          
          char choice_selection;
          choice_selection = getchar();  
          
     
       
       switch(choice_selection)
               {
               case ('A'):
                     {
               
                     int c_choice;
                     char c_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(c_Col_line, sizeof(c_Col_line), stdin);
                     sscanf(c_Col_line, "%d", &c_choice);
                     
                     if(c_choice*c_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     //goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (c_choice*c_price));
                     *my_c_units_ptr = c_choice;
                     }
                     }
                     break;
       
       
               case ('B'):
                     {
                     int h_choice;
                     char h_Col_line[100];
                     printf("How Much? \n");
                     
                     fgets(h_Col_line, sizeof(h_Col_line), stdin);
                     sscanf(h_Col_line, "%d", &h_choice);
                     
                     if(h_choice*h_price > *my_money_ptr)
                     {
                     printf("You don't have enough Money!\n");
                     printf("Please try again.....\n");
                     //goto start_buy;
                     }
                     else
                     {
                     *my_money_ptr = (*my_money_ptr - (h_choice * h_price));
                     *my_h_units_ptr = h_choice;
                     }
                     }
                     break;
                case ('C'):
                     //goto start_buy;
                     break;
       
               case ('D'):
                     //goto start_buy;
                     break;
    
    
    
    
    
    
               case ('E'): 
                     //goto start_buy;
                     break;
    
    
    
    
    
    
               case ('F'): 
                     //goto start_buy;   
                     break;
    
    
               case ('G'): 
                     //goto start_buy;    
                     break;
               default :
                       //goto start_buy;
                 printf("Default\n");
                 break;
      /* try again */
       }   
              
         }

  5. #5
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    I added break and it still flew right through

  6. #6
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    Are you entering a lowercase letter? You compare against uppercase, and 'a' does not equal 'A'. You might try something like this:
    Code:
    #include <ctype.h>   // with your other includes
    // ...
    choice_selection = getchar();
    if (islower(choice_selection))
        choice_selection = toupper(choice_selection);
    You'll probably run into problems related to the newline left behind, but that's another story.
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  7. #7
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    You might have some extra characters hanging out in the input buffer, being picked up by your "getchar()", though it would be from some time before this function is called.

    Have you tried placing something to eat up any extra characters in the input buffer before prompting the user for input? Perhaps something like this before the "getchar()" is called might help in your case:

    Code:
    while(getchar() != '\n');

  8. #8
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    I'm actually not even given an opportunity to enter a char

  9. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Do you mean at line 23, or anywhere in your program up to that point?

  10. #10
    Registered User
    Join Date
    May 2012
    Location
    Milwaukee
    Posts
    28
    Yes I go into the function and it runs right through to the main function without stopping or asking for any input

  11. #11
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    1. It would be easier if you didn't answer an "or" question with a "yes."

    2. I told you that you might have some extra characters floating in the input buffer being picked up by your "getchar()". You say that you never have an opportunity to execute that "getchar()". That is exactly the point. Which brings me to...

    3. Have you tried what I suggested in post #7?

  12. #12
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Are you able to compile this program, With having redeclaration of 'my_p_units_ptr' and 'p_price'! Seems some serious issues with your compiler. Change the compiler first.
    S_ccess is waiting for u. Go Ahead, put u there.

  13. #13
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    The problem of extra char's in the input buffer, sounds spot on, and is a VERY common problem, since we can't see exactly what the input buffer holds. Consider these two snippets of code:

    Code:
    #include <stdio.h>
    
    int main(void) {
       char choice = 'a';
       int n = 0;
    
       while (choice != 'q') {
          printf("Enter a letter (or q to quit) : ");
          scanf("%c", &choice);
          printf("You entered %c, and n now equals %d\n",choice, n++);
       }    
    
       return 0;
    }
    Run that, and watch what the value of n becomes, as it loops around. It should show your choice of letter, and 0,1,2,3, etc., but it won't. It will skip your choice sometimes, just like your program is doing, and the reason is that the newline char ('\n'), is being read from the input buffer, as a char, sometimes. (The n is there just to show you that the loop is indeed executing -- but your choice is being "skipped".

    Now try this, which fixes the problem:

    Code:
    #include <stdio.h>
    
    int main(void) {
       char choice = 'a';
       int n = 0;
    
       while (choice != 'q') {
          printf("Enter a letter (or q to quit) : ");
          scanf("%c ", &choice);     //note the space now, right after the %c - that handles the char immediately after your char
          printf("You entered %c, and n now equals %d\n",choice, n++);  //effectively discarding the troublesome newline, from the input buffer
       }    
    
       return 0;
    }
    
    //Another way of doing the same thing:
    
    #include <stdio.h>
    
    int main(void) {
       char choice = 'a';
       int n = 0;
    
       while (choice != 'q') {
          printf("Enter a letter (or q to quit) : ");
          scanf("%c", &choice);
          getchar(); //pulls the newline char from the input buffer, one getchar() pulls one char only.
          printf("You entered %c, and n now equals %d\n",choice, n++);
       }    
    
       return 0;
    }
    For this problem, a change of compilers won't matter. The input buffer is YOUR responsibility, in C.

  14. #14
    Registered User
    Join Date
    Dec 2005
    Posts
    136
    Quote Originally Posted by Adak View Post
    For this problem, a change of compilers won't matter. The input buffer is YOUR responsibility, in C.
    U have explained exact solution of the problem well, but still change of compiler is required, because of the code which is pasted has redeclaration of 'my_p_units_ptr' and 'p_price' and still user is able to compile the program and execute it!!
    S_ccess is waiting for u. Go Ahead, put u there.

  15. #15
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    Thus the "For THIS problem". Not for all the other problems -- I'm leaving them for YOU!

    <grin>

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 16
    Last Post: 11-30-2011, 09:59 AM
  2. Switch statement
    By Programmer_P in forum C++ Programming
    Replies: 9
    Last Post: 07-24-2010, 03:11 AM
  3. Compiler skipping a statement & moving to nxt instruction
    By MarjunC in forum C++ Programming
    Replies: 8
    Last Post: 06-29-2010, 05:34 AM
  4. Skipping if statement
    By FallenBlade in forum C Programming
    Replies: 6
    Last Post: 12-13-2009, 03:40 PM
  5. Switch statement
    By BSmith4740 in forum C Programming
    Replies: 29
    Last Post: 02-28-2008, 08:28 PM