weird scanf()

This is a discussion on weird scanf() within the C Programming forums, part of the General Programming Boards category; take a look at this piece of code... Code: #include <stdio.h> int main( void ) { int i = 0, ...

  1. #1
    Registered User trekker's Avatar
    Join Date
    Mar 2002
    Posts
    46

    weird scanf()

    take a look at this piece of code...

    Code:
    #include <stdio.h>
    
    int main( void )
    {
    	int i = 0, c = 0, sent = 0;
    	char str[ 30 ];
    
    	printf( "Choose ( 0 to this, 1 do that ) : " );
    	scanf( "%d", &sent );
    
    	if( sent )
    		;/* do stuff */
    	else {
    		printf( "Please input a string of text : \n" );
    		
    		while( ( c = getchar() ) != '\n' )
    			str[ i++ ] = c;
    
    		str[ i ] = '\0';
    
    		printf( "The string entered was : \n" );
    		puts( str );
    	}
    	
    	return 0;
    }
    if the sentinel value is 0 it seems that getchar() takes
    the newline char that i inputed in order to scanf() the sentinel
    value.
    I thought that scanf() doesn't leave the newline char in stdin,
    so that i don't have to fflush() it.

    Any comments on that ?

    trekker
    to boldy code where...

  2. #2
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,796
    >I thought that scanf() doesn't leave the newline char in stdin
    Yes, it does. You can read about many people's woes by searching for the many hundreds of posts about scanf in the board search utility.

    >so that i don't have to fflush() it.
    You'd better not anyway, fflush is not defined for input streams. When I say that most people seem to shrug it off as useless information so I'll rephrase it. Using fflush ( stdin ); is wrong and can ruin your computer. That's a worst case scenario, but undefined behavior brings this into the realm of possibility. Try something along the lines of this:
    Code:
    #include <stdio.h>
    #define FLUSH_STDIN while ( getchar() != '\n' )
    
    int main( void )
    {
    	int i = 0, c = 0, sent = 0;
    	char str[ 30 ];
    
    	printf( "Choose ( 0 to this, 1 do that ) : " );
    	scanf( "%d", &sent );
             FLUSH_STDIN;
    
    	if( sent )
    		;/* do stuff */
    	else {
    		printf( "Please input a string of text : " );
    		
    		while( ( c = getchar() ) != '\n' )
    			str[ i++ ] = c;
    
    		str[ i ] = '\0';
    
    		printf( "The string entered was : " );
    		puts( str );
    	}
    
    	return 0;
    }
    -Prelude
    My best code is written with the delete key.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. weird bug in the scanf command
    By transgalactic2 in forum C Programming
    Replies: 13
    Last Post: 10-24-2008, 03:26 PM
  2. Weird output of scanf and printf.
    By omnificient in forum C Programming
    Replies: 2
    Last Post: 12-05-2007, 12:28 PM
  3. Replies: 2
    Last Post: 02-20-2005, 12:48 PM
  4. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 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

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21