Thread: getchar won't take any input after scanf

  1. #1
    Registered User
    Join Date
    Oct 2008
    Posts
    30

    Question getchar won't take any input after scanf

    hello,

    Its a come back for me to this forum after a long time,
    I have got this weird problem that getchar wont function after two successive scanf's
    Code:
    #include <stdio.h>
    main(){
    	int a;
    	int b;
    	int c;
    	scanf("%d",&a);
    	scanf("%d",&b);
    	while((c=getchar())!='\n')
    		putchar(c);
    	printf("%d %d",a,b);
    }
    I do know that scanf doesn't flush input stream, and can i get any other neat way to code this. where i can even take the input from the get char. i observed the same problem even while using "fgets" instead of getchar


    thanks in advance

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    This question happened yesturday, and it often happens a few times a week. This is because scanf("%d") will leave a '\n' in the input buffer. Read this thread, there are a few good solutions offered and explained:

    fgetchar() doesnt work
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Oct 2008
    Posts
    30
    Thanks MK27

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf, getchar, gets, etc. are ignored.
    By daltore in forum C Programming
    Replies: 19
    Last Post: 12-27-2007, 10:47 PM
  2. flushall()
    By salvadoravi in forum C Programming
    Replies: 21
    Last Post: 12-24-2007, 12:39 PM
  3. Restrict scanf() input?
    By difficult.name in forum C Programming
    Replies: 4
    Last Post: 09-20-2004, 12:27 PM
  4. Problem with scanf input
    By ejpjoker in forum C Programming
    Replies: 4
    Last Post: 04-29-2004, 08:28 PM
  5. help with input and scanf
    By kpw in forum C Programming
    Replies: 9
    Last Post: 08-28-2001, 08:21 PM

Tags for this Thread