Thread: Second scanf keeps getting skipped.

  1. #1
    Registered User
    Join Date
    May 2006
    Posts
    182

    Second scanf keeps getting skipped.

    The title pretty much explains my problem.
    Yes, I know I shouldn't be using scanf.

    Code:
    #include <stdio.h>
    unsigned int packCharacters( char a, char b );
    void displayBits( int number );
    void displayBitsC( char a );
    
    int main()
    {
    	int i = 1;
    	printf( "%d\n", sizeof( char ) );
    	printf( "%d\n", sizeof( unsigned int ) );
    	printf( "%c\n", i );
    
    	char a, b;
    
    	printf( "Enter a character: " );
    	scanf( "%c", &a );
    	printf( "Enter another character: " );
    	scanf( "%c", &b );
    
    	displayBits( (int)a );
    	displayBits( (int)b );
    	displayBits( packCharacters( a, b ) );
    	return 0;
    }
    
    
    
    
    
    unsigned int packCharacters( char a, char b )
    {
    	unsigned int combo;
    
    
    	combo = a << 8;
    
    	return ( combo | (unsigned int)b );
    }

    Code:
    eugene@eugene-laptop:~/cfiles$ ./a.out
    1
    4
    
    Enter a character: a
    Enter another character: 0000 0000 0000 0000 0000 0000 0110 0001 
    0000 0000 0000 0000 0000 0000 0000 1010 
    0000 0000 0000 0000 0110 0001 0000 1010

  2. #2
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    That's because of '\n' character is stored in b as you press enter.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  3. #3
    Registered User
    Join Date
    May 2006
    Posts
    182
    hmm very strange. Thanks.

  4. #4
    HelpingYouHelpUsHelpUsAll
    Join Date
    Dec 2007
    Location
    In your nightmares
    Posts
    223
    Quote Originally Posted by yougene View Post
    hmm very strange. Thanks.
    Not really that strange. scanf is used to get an array of characters (since you have to press ENTER to stop entering characters). There are other functions to get one character from input, perhaps you should look them up.
    long time no C; //seige
    You miss 100% of the people you don't C;
    Code:
    if (language != LANG_C && language != LANG_CPP)
        drown(language);

  5. #5
    and the hat of copycat stevesmithx's Avatar
    Join Date
    Sep 2007
    Posts
    587
    hmm very strange. Thanks.
    I think i should have been more specific.
    '\n' character is stored in b as you press enter after the previous character input a.
    Not everything that can be counted counts, and not everything that counts can be counted
    - Albert Einstein.


    No programming language is perfect. There is not even a single best language; there are only languages well suited or perhaps poorly suited for particular purposes.
    - Herbert Mayer

  6. #6
    Registered User
    Join Date
    Nov 2008
    Location
    Phoenix
    Posts
    70
    Code:
    	printf( "%c\n", i );
    I think you meant to put %d there, since you declared i as an int. (I ran your code as-is and the results printed a happy-face smiley for that line, haha.)

    As for making it do the second scanf instead of skipping it, changing the line so that it looks like this should work:

    Code:
    	scanf( " %c", &b );
    Note the space between the first " and the %, which is the only change. As for why that works, I can't recall at the moment. What I do recall though, is that doing this is only necessary for chars, and not ints (I think...).

  7. #7
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Note the space between the first " and the %, which is the only change. As for why that works, I can't recall at the moment. What I do recall though, is that doing this is only necessary for chars, and not ints
    scanf() is great. Any whitespace in scanf() tells it to read as much whitespace as possible; that means that " %c" will eat any newlines (or other whitespace) that may be hanging around in standard input. Incidentally, it also means you can hit enter as much as you want and it will happily keep eating that whitespace.

    It's not needed for ints because scanf(), in that case, already skips past whitespace in the input. With " %c" you're essentially telling it to mimic the behavior of "%d", etc.

    The "f" in "scanf" means "formatted", and it shows. If you feed scanf() any input it's not expecting, it tends to explode.

  8. #8
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Quote Originally Posted by cas View Post
    scanf() is great. Any whitespace in scanf() tells it to read as much whitespace as possible; that means that " %c" will eat any newlines (or other whitespace) that may be hanging around in standard input. Incidentally, it also means you can hit enter as much as you want and it will happily keep eating that whitespace.
    Mostly true except for the %c conversion specification as it does not eat whitespace so something like scanf( "%c%*c", &a ) works as it discards the trailing newline.

  9. #9
    Registered User
    Join Date
    Oct 2008
    Posts
    115
    flushing the stdin after the scanf will do..

  10. #10
    Registered User
    Join Date
    Sep 2007
    Posts
    1,012
    Replying to two posts:
    Mostly true except for the %c conversion specification as it does not eat whitespace so something like scanf( "%c%*c", &a ) works as it discards the trailing newline.
    Perhaps the use of a proportional font made you miss the space in front of the %c in my and Sorin's examples. " %c" (note the space) does eat whitespace. It then reads a (non-whitespace by necessity) character. Assuming, of course, it doesn't run into a problem like EOF.

    Both methods (discarding whitespace before reading and discarding it after) have their own problems. C'est la vie with scanf().
    flushing the stdin after the scanf will do..
    That is very non-portable. According to the standard, fflush(stdin) is undefined behavior. On my system it does not empty the input stream as (I presume) it does on yours.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. scanf() consideres useless
    By Snafuist in forum C Programming
    Replies: 15
    Last Post: 02-18-2009, 08:35 AM
  2. scanf is being skipped
    By yougene in forum C Programming
    Replies: 6
    Last Post: 12-24-2008, 06:05 AM
  3. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 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