Thread: clearing scanf

  1. #1
    Registered User
    Join Date
    Jan 2002
    Posts
    5

    Question clearing scanf

    In c++ you can clear out the io stream with the cin.ignore(). What do you use with scanf()? my problem is when a user enters in two numbers (space between numbers) when prompted for one. The second number is picked up by the next scanf()....I want to clear it out of the stream! Any ideas?

    simple example:

    #include <cstdio>
    int main ()
    {
    int num=0;
    while (true)
    {
    printf("enter number (-1 to exit)\n");
    scanf("%d", &num);
    if (num==-1)
    break;
    printf("entered = %d \n", num);
    }
    return 0;
    }

  2. #2
    Registered User
    Join Date
    Jan 2002
    Posts
    5
    how simple!!! Thanks!

  3. #3
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    It's usually just the newline character that is left in the standard input buffer:

    scanf("%n %*",&iNum);

    That will work in most cases as well as the above.

    In C++ isn't it:

    cin.clear();

    ?

  4. #4
    Registered User
    Join Date
    Jan 2002
    Posts
    5
    cin.clear() clears the eof, bad, fail and good status bits

  5. #5
    Registered User moonwalker's Avatar
    Join Date
    Jul 2002
    Posts
    282

    question...

    dumb question... but...
    why did it work ??

    Code:
    #include <stdio.h>
    
    int main ()
    {
    	int num=0;
    	while (true)
    	{
    		printf("enter number (-1 to exit)\n");
    		scanf("%d", &num);
    		
    		while ( getchar() != '\n' );
    
    		if (num==-1)
    		{
    			break;
    		}
    
    		printf("entered = %d \n", num);
    
    	}
    
    	return 0;
    }
    what does this:
    while ( getchar() != '\n' );

    have to do with two numbers seperated by spaces and the second number being ignored ?

  6. #6
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231

    Re: question...

    Originally posted by moonwalker
    dumb question... but...
    why did it work ??
    <snip code>
    what does this:
    while ( getchar() != '\n' );

    have to do with two numbers seperated by spaces and the second number being ignored ?
    The loop
    >>>while ( getchar() != '\n' );
    will basically eat any input in the buffer up until the newline character.

    With your code, if you entered the following as input on the command prompt:
    12 17 -1
    The scanf() function would read the first number and assign it to the variable. The while loop would then eat the 17 and -1, so the rest of your code never sees it.
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  2. clearing buffer after reading string w/ scanf()
    By fisheromen1031 in forum C Programming
    Replies: 11
    Last Post: 08-01-2005, 09:33 AM
  3. Replies: 2
    Last Post: 02-20-2005, 01:48 PM
  4. Scanf and integer...
    By penny in forum C Programming
    Replies: 3
    Last Post: 04-24-2003, 06:36 AM
  5. scanf - data is "put back" - screws up next scanf
    By voltson in forum C Programming
    Replies: 10
    Last Post: 10-14-2002, 04:34 AM