Thread: I'm trying to get more than one getchar() function to work, but it's skipped over.

  1. #1
    Registered User
    Join Date
    Sep 2018
    Posts
    4

    I'm trying to get more than one getchar() function to work, but it's skipped over.

    My code won't let me use more than one getchar() function:

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    //SamsFirst_IO.c
    
    
    int main(){
        char x,y;
        printf("Type a letter: ");
        x = getchar();
        printf("Type another letter: ");
        y = getchar();
        //printf("You typed '%c'.\n",c);
        putchar('\n');
        printf("You typed ");
        putchar(x);
        printf(" and ");
        putchar(y);
        //printf(".\n");
        
        return(0);
    }
    For some reason, the console fails to let me use the
    Code:
    y = getchar();
    After typing in the first letter and hitting enter, it skips straight to this:
    I'm trying to get more than one getchar() function to work, but it's skipped over.-000-png
    Why does it do that?

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,658
    > Why does it do that?
    Because the \n you typed after 'h' is also something that getchar() will return to you.

    If you typed 'hi' and then enter, it would do as you want.
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 4
    Last Post: 08-07-2018, 08:21 AM
  2. Replies: 4
    Last Post: 09-24-2015, 01:51 AM
  3. My setup() function is being skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-23-2015, 01:47 PM
  4. My setup() function is skipped.
    By howardbc14 in forum C Programming
    Replies: 3
    Last Post: 05-14-2015, 11:29 PM
  5. gets function skipped 1st time
    By ambrown782 in forum C Programming
    Replies: 6
    Last Post: 07-26-2014, 12:30 AM

Tags for this Thread