fgetc() and Switch-Case - Executed twice?

This is a discussion on fgetc() and Switch-Case - Executed twice? within the C Programming forums, part of the General Programming Boards category; Hi Guys, Im trying out C inputs. But the following code seems to be running out of expectations. When the ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    41

    fgetc() and Switch-Case - Executed twice?

    Hi Guys,

    Im trying out C inputs. But the following code seems to be running out of expectations.

    When the program runs, it asked for 1,2,3. I entered 1. It should print out "1". Then asking me for another input again.

    But judging from the output after i pressed 1, "1" condition is executed, then the menu pops up again, and seems that a return key is hit automatically which will incur the "default" statement. Then the menu pops up again for my input again.

    Weird. Can anyone enlighten me please? Thanks.

    The output here shows what the user will see on screen when a user run the program ONCE. Here is the output:
    Code:
       What do you want to do?[1,2,3]:1
    1
       What do you want to do?[1,2,3]:
       Please Select  number between 1-3.
    
       What do you want to do?[1,2,3]:
    Code:
    #include <stdlib.h>
    #include <stdio.h>
    
    int main()
    {
    
      int select;
      
      while(1)
      {
       printf("\n   What do you want to do?[1,2,3]:");
       select = fgetc(stdin)-'0';
      switch(select)
      {
        case 1:
             printf("1");
             break;
        case 2:
             printf("2");
             break;
        case 3:
             printf("3");
             break;
        default:
         printf("\n   Please Select  number between 1-3.\n");
         break;
        
      }
      }
    }
    Last edited by stevong; 11-26-2005 at 11:29 PM.
    If I'm able to turn back time, I would learn C as my first language.

  2. #2
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Sorry. Topic should read fgetc() instead of fgets()
    If I'm able to turn back time, I would learn C as my first language.

  3. #3
    Registered User
    Join Date
    Nov 2004
    Location
    India
    Posts
    515
    What do you have that -'0' for? Remove it. The compiler will consider the ascii value of the inputted character and subtract the ascii value of '0' from it. You can simply use the character value itself. Your cases should be case '1' instead of case 1.
    This will work equally well.

    also, you should be clearing the input buffer after the call to fgetc as it retains the extra characters on the buffer. Please read the FAQ for that.
    Last edited by PING; 11-27-2005 at 01:25 AM.
    Code:
    >+++++++++[<++++++++>-]<.>+++++++[<++++>-]<+.+++++++..+++.[-]>++++++++[<++++>-] <.>+++++++++++[<++++++++>-]<-.--------.+++.------.--------.[-]>++++++++[<++++>- ]<+.[-]++++++++++.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    41
    Oh yes. You r right. . The \n is causing the problem.

    Managed to solve the "double execution" by using

    while(fgetc(stdin)!='\n');


    i used -'0' because i want to cast the char back to int for the switch case statement ? Since fgetc will return me a value of ascii 49 if i input 1 from the keyboard.

    I wanted the switch case to use int. You r right.
    I can simply use char though. Looks like Im being redundant by doing the conversion.
    If I'm able to turn back time, I would learn C as my first language.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need to access data outside a function
    By eternities_end in forum C Programming
    Replies: 3
    Last Post: 10-16-2005, 07:35 PM
  2. logo programming
    By datainjector in forum C Programming
    Replies: 12
    Last Post: 08-19-2002, 05:37 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21