Thread: Some help with switch case bug...

  1. #1
    Registered User
    Join Date
    Mar 2005
    Posts
    15

    Some help with switch case bug...

    Hey there...I discovered some bugs with my program and hope that you all will help me out with it...Its a very simple program actually but there's a little problem.....

    First of all, this is the source code:
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void function1();
    void function2();
     
    int main()
    {
      char option[2];
    
      do  
      {
        printf("\n1. Function 1\n");
        printf("2. Function 2\n");
        printf("3. Exit\n");
       
        printf("\nEnter an option: ");
        gets(option);
    
        printf("\n");
    
        switch(option[0])
        {
          case '1': 
            function1();
            break;
          case '2': 
            function2();
            break;
          case '3':
            printf("Exiting program.....\n");
            exit(-1);
          default:
            printf("Incorrect input!! Please enter 1-3");
            break;
        }
      }while(option[0] != '3');
    }
    
    void function1()
    {
      int number1;
    
      printf("Enter a number: ");
      scanf("%d",&number1);
    }
    
    void function2()
    {
      char string[100];
      char string2[100];
    
      printf("Enter first string: ");
      gets(string);
    
      printf("Enter second string: ");
      gets(string2);
    }
    This is the output:
    Code:
    /*First time print of Menu */
    1. Function 1
    2. Function 2
    3. Exit
    
    /* First input to select an option*/
    Enter an option: 1
    
    /* Performing function1() */
    Enter a number: 20
    
    /* Second time print of Menu */
    1. Function 1
    2. Function 2
    3. Exit
    
    /* This is the bug part...It will automatically skip my option input and display the error msg... */
    Enter an option:
    Incorrect Input! Please enter 1-3
    
    /*Third print of Menu*/
    1. Function 1
    2. Function 2
    3. Exit
    
    Enter an option: 3
    
    Exiting Program......
    
    Press any key to continue......
    So erm...I've noted where the bug is in in the example output...Hope you all can understand and help me out with it...Thanks...

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    It's because of your usage of scanf(). scanf() leaves the '\n' from when you press ENTER in the buffer so gets() sees that and thinks you just pressed ENTER at the choice prompt. You can either use something besides scanf() or clear the input buffer after using it.

    By the way, gets() is dangerous and should never be used. fgets() is a safe replacement. The reason gets() shouldn't be used is described in the FAQ on this site.
    Last edited by itsme86; 05-17-2005 at 10:33 AM.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User
    Join Date
    Jan 2002
    Location
    Vancouver
    Posts
    2,212
    char option;

    ...

    scanf("%c\n", &option);

    ...

    switch(option)

  4. #4
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,218
    That would still cause problems with function2()...
    If you understand what you're doing, you're not learning anything.

  5. #5
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > scanf("%c\n", &option);
    Which leads to even more weirdness than normal scanf usage. %c is a rule unto itself compared to all the other format conversions.

    Stick to fgets(), then use sscanf if that's appropriate

    > char option[2];
    char option[BUFSIZ];
    There is little reason to make primary input buffers anything other than BUFSIZ in size.

    > gets(option);
    > switch(option[0])
    Then you do
    fgets( option, sizeof option, stdin );
    switch(option[0])

    > scanf("%d",&number1);
    Would be
    char buff[BUFSIZ];
    fgets( buff, sizeof buff, stdin );
    sscanf( buff, "%d",&number1);
    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.

  6. #6
    Registered User
    Join Date
    Mar 2005
    Posts
    15
    Thanks a lot for the help...It works fine now~!! I'll keep in mind not to use gets() anymore and stick with fgets() and sscanf()~!! Thanks again~!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Number to Word (Billions)
    By myphilosofi in forum C Programming
    Replies: 34
    Last Post: 02-04-2009, 02:09 AM
  2. Base converter libary
    By cdonlan in forum C++ Programming
    Replies: 22
    Last Post: 05-15-2005, 01:11 AM
  3. Reducing Code size from ridiculous length
    By DanFraser in forum C# Programming
    Replies: 10
    Last Post: 01-18-2005, 05:50 PM
  4. Xmas competitions
    By Salem in forum Contests Board
    Replies: 88
    Last Post: 01-03-2004, 02:08 PM
  5. Switch Case
    By FromHolland in forum C++ Programming
    Replies: 7
    Last Post: 06-13-2003, 03:51 AM