printf and scanf command

This is a discussion on printf and scanf command within the C++ Programming forums, part of the General Programming Boards category; I was wondering if anyone could tell me why the second scanf is skipped after i enter one number and ...

  1. #1
    Registered User
    Join Date
    Jul 2004
    Posts
    222

    printf and scanf command

    I was wondering if anyone could tell me why the second scanf is skipped after i enter one number and then hit enter? I am trying read two values from the keyboard and send them through a pipeline. Does anyone know which function to call for the program to store the keyboard character that is pressed to a char variable right after the key has been pressed without a following enter (line carriage)? Thanks!


    Code:
    	while(1)
    	{
    		printf("Please select the elevator car letter (A, B, C) or Up/Down arrows : ");
    		scanf("%c", &temp);
    		printf("%c\n", temp);
    		// Add character append function calls
    		printf("Please enter the floor number (0 - 9) : ");
    		scanf("%c", &temp);
    		printf("%c\n", temp);
    		// Add character append function calls
    	}

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    It's because scanf()'s %c matches newlines as well as ordinary characters. When you tell scanf to read a number and enter "123\n", it takes the "123" and leaves the "\n" for the next scanf to read when looking for a character.

    There are lots of ways around this problem. The best is probably to read input as lines with fgets() and then use sscanf() to parse those lines.
    Code:
    char buffer[BUFSIZ];
    
    fgets(buffer, sizeof(buffer), stdin);
    sscanf(buffer, "%c", &c);
    
    /* or, with error checking: */
    if(!fgets(buffer, sizeof(buffer), stdin);
        || sscanf(buffer, "%c", &c) != 1) {
    
        perror("Error reading stdin");
    }
    Another way is to simply discard any trailing whitespace between scanf()s. You can do this with a single
    Code:
    getchar();
    or better yet, in case there are multiple newlines or spaces:
    Code:
    while(getchar() != '\n');
    BTW -- this is the C++ forum, and cin and cout should be used in C++ instead of scanf() and printf().
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    Note that this is C, not C++. And of course, the second scanf isn't skipped -- there's a perfectly good character waiting to be input, namely the enter key from the first time. You can do another scanf, if you like, or to actually do it in a reasonable way, do scanf("%d"), since scanf skips whitespace when looking for numbers. (Of course, temp would then need to be an int and not a char.)

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 2
    Last Post: 03-05-2009, 09:25 AM
  2. Data Structure Eror
    By prominababy in forum C Programming
    Replies: 3
    Last Post: 01-06-2009, 08:35 AM
  3. Newb Help: Full Arrays and Functions
    By LycanGalen in forum C Programming
    Replies: 5
    Last Post: 01-31-2008, 07:35 PM
  4. Please help debug
    By Wexy in forum C Programming
    Replies: 2
    Last Post: 11-10-2002, 11:40 PM
  5. help with switch statements
    By Wexy in forum C Programming
    Replies: 3
    Last Post: 11-06-2002, 04:44 PM

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