Thread: getchar() overpasses next input

  1. #1
    Registered User
    Join Date
    Jan 2016
    Posts
    4

    getchar() overpasses next input

    i try to use getchar to get a single character but when ever i use it it will block the very next input
    using this code
    Code:
    #include<stdio.h>
    void main (){
        char c;
        c = getchar();
        putchar(c);
        c = getchar();
        putchar(c);
        printf("End\n");
    }
    
    will take only one input, also if i replaced the second getchar() with any othe input like gets() the same problem occur.
    This also happens with scanf() , so how can i prevent this from happenning.



  2. #2
    Registered User camel-man's Avatar
    Join Date
    Jan 2011
    Location
    Under the moon
    Posts
    693
    The second getchar is picking up the newline character left in the buffer after the first getchar().

    For example you enter in

    - "A\n"
    first getchar grabs the 'A' then the second getchar grabs the '\n' (this is from hitting the enter key)


    There is a neat little trick where you can use scanf(" %c",&ch);

    notice the space BEFORE the % sign. This will tell scanf to ignore any whitespace (includes newlines).
    Last edited by camel-man; 02-04-2016 at 11:42 AM.
    Code:
    int get_random_number(void)
    {
       return 4; //chosen by fair dice roll.
                 //guaranteed to be random
    }

  3. #3
    Registered User
    Join Date
    Jan 2016
    Posts
    4
    Quote Originally Posted by camel-man View Post
    The second getchar is picking up the newline character left in the buffer after the first getchar().

    For example you enter in

    - "A\n"
    first getchar grabs the 'A' then the second getchar grabs the '\n' (this is from hitting the enter key)


    There is a neat little trick where you can use scanf(" %c",&ch);

    notice the space BEFORE the % sign. This will tell scanf to ignore any whitespace (includes newlines).
    OK i know that getchar() take a second order when i press enter , so how i prevent this i don't want this to happen.
    then ,notice this code

    Code:
    #include<stdio.h>
    void main (){
        int i;
        char arr[50];
        printf("put a number\n");
        scanf("%d\n", &i);
        printf("%d\n",i);
        printf("put a txt\n");
        gets(arr);
        puts(arr);
    
    
    }

    it will accept digits and skips the gets() function.

  4. #4
    Registered User
    Join Date
    Jun 2011
    Posts
    4,513
    Don't use "gets()"! >> FAQ > Why gets() is bad / Buffer Overflows - Cprogramming.com

    The lazy option is to flush the input buffer, such as with a "while()" loop: FAQ > Flush the input buffer - Cprogramming.com

    The better option is to just read an entire line at a time (fgets), and extract the information out of the resulting string (sscanf, strtol, etc).

    And "main()" returns int, not void: FAQ > main() / void main() / int main() / int main(void) / int main(int argc, char *argv[]) - Cprogramming.com

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 12-03-2015, 03:19 AM
  2. getchar - multiple input problems
    By Dregg in forum C Programming
    Replies: 6
    Last Post: 03-21-2012, 04:43 AM
  3. understanding getchar(), buffered user input, and so on...
    By FernandoBasso in forum C Programming
    Replies: 4
    Last Post: 10-06-2011, 04:12 PM
  4. getchar() doesn't wait for input
    By cantinero74 in forum C Programming
    Replies: 5
    Last Post: 04-27-2010, 09:46 AM
  5. getchar won't take any input after scanf
    By pshirishreddy in forum C Programming
    Replies: 2
    Last Post: 08-02-2009, 11:46 AM

Tags for this Thread