Thread: else wont read input?

  1. #1
    Registered User King_of_Hearts's Avatar
    Join Date
    Apr 2010
    Location
    connecticut
    Posts
    3

    Question else wont read input?

    Hi, im learning C and am currently learning about single character variables and such.
    Now i know a little about IF statements but for some reason the last conditional statement
    "the else" wont read the keyboard for a single character value whether i use scanf OR getchar. Can anyone spot a flaw? its an extremely simple code. thanks in advance.

    Code:
     #include <stdio.h>
    #include <stdlib.h>
    int main()
    {
        char ch1;
        char ch2;
        char ch3;
        
        int teger1;
        int teger2;
        int teger3;
        
        printf("\tA. Me break you neck!\n\
    \tB. Me electrocute you!\n\
    \tC. Me kiss you!\n");
        printf("Choose a task:");
        ch1=getchar();     
             
                
        if (ch1=='A'||ch1=='a')
        {
                    printf("CRRRACK!\n\n");
                    }
                    
         else if(ch1=='B'||ch1=='b')
         {
              printf("BZZZZZZZZT!\n\n");
              }          
                
                
            else if(ch1=='C'||ch1=='c')
            {
                 printf("MWAH!\x03\n\n");
                 }
                   
            else 
                {
                printf("\t\tInvalid Entry!\n\t\tTry Again?");
                printf("\n\n\t\tY/N");
                //getchar();                            /*<--input not being read...why?*/
                scanf("%c",&ch2);
                }
                             printf("\n\n"); 
                              system("pause");
                              return(0);
                              }
    Im trying to get it to allow the user to select whether they want to try again or not.
    The program runs fine with no errors it just doesn't prepare to read input. it just ends..?

    update: the problem was that the enter key was being read right after the character i input.

    Solution:

    Code:
             printf("\tA. Me break you neck!\n\
    \tB. Me electrocute you!\n\
    \tC. Me kiss you!\n");
        printf("Choose a task:");
        ch1=getchar();     
        getchar();        /*<----A stray getchar() absorbs the enter key so the next getchar() doesnt pick it up by mistake*/
    Last edited by King_of_Hearts; 05-02-2010 at 05:28 AM. Reason: SOLVED

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    It's probably because when you press A, you also hit ENTER, which is a character. Therefore, you're actually entering two characters, and so the second call to getchar picks that up.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User King_of_Hearts's Avatar
    Join Date
    Apr 2010
    Location
    connecticut
    Posts
    3
    Man that must be it because when i add a getchar() b4 ch2=getchar it works fine.
    it must absorb that enter key. So i cant put 2 getchars one after the other?Ok.
    now i can figure out a solution. thanks a billion quzah.
    Last edited by King_of_Hearts; 05-02-2010 at 05:16 AM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. IDE for C embedded advanced editing
    By Undici77 in forum Tech Board
    Replies: 32
    Last Post: 01-16-2010, 05:17 PM
  2. Replies: 8
    Last Post: 12-08-2009, 12:55 PM
  3. bytes lost with partial read in UDP
    By mynickmynick in forum Networking/Device Communication
    Replies: 3
    Last Post: 03-31-2009, 02:06 AM
  4. trying to read input into an array
    By trprince in forum C Programming
    Replies: 16
    Last Post: 11-17-2007, 06:20 PM
  5. Help with ! (NOT)
    By Skarjak in forum C++ Programming
    Replies: 4
    Last Post: 02-18-2005, 08:25 PM

Tags for this Thread