Thread: Scanf

  1. #16
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Quote Originally Posted by Salem View Post
    Well you can't just use scanf(), because you're trying to treat one of the inputs as optional.
    Quote Originally Posted by vikasvijayan View Post
    by using the single scanf(),you cannot terminate the pgm by inputting one zero
    Code:
    #include<stdio.h>
    int main( void )
    {
        int done = 0;
        do
        {
            int a, b;
            
            printf( "enter #,# or 0 to exit: " );
            fflush( stdout );
            
            switch( scanf( "%d,%d", &a, &b ) )
            {
                case 2: printf( "a: %d, b: %d\n", a, b ); break;
                case 1: if( a == 0 ) { done = 1; break; }
                default: /* error */ scanf( " %*[^\n]%*c"  );
            }
        }
        while( !done );
        return 0;
    }
    :eyebrow:

    Edit - You can still make it slightly amiss with your input, but it doesn't break.


    Quzah.
    Last edited by quzah; 10-03-2011 at 03:48 AM. Reason: %*c = works just a touch better
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. \n after scanf
    By shonit in forum C Programming
    Replies: 2
    Last Post: 03-04-2008, 07:52 AM
  2. while scanf
    By PUI in forum C Programming
    Replies: 3
    Last Post: 10-06-2006, 11:24 AM
  3. First scanf() skips next scanf() !
    By grahampatten in forum C Programming
    Replies: 5
    Last Post: 08-17-2004, 02:47 AM
  4. 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
  5. scanf
    By jwhitaker3 in forum C Programming
    Replies: 2
    Last Post: 09-06-2002, 12:52 PM