Thread: scanf "ignored"

  1. #1
    Registered User
    Join Date
    Sep 2007
    Location
    Reggio Emilia, Italy
    Posts
    15

    scanf "ignored"

    Hello everybody,

    an exercises of mine: it's a "guess the number" game.
    The repetition of the game is given by a char variable: when the player guess the number I aks him/her if he/she want to play again, but the relative scanf is "ignored" and I don't see why.

    I putted two additional printf to control number (no time to guess every time I make a try to fix the bug) and to control the value of the repetition char variable.

    This 's the code...



    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <time.h>
    
    int not_guessed(int real_number, int player_guess);
    
    
    int main (void)
    {/************/
    
       int number, guess;
       char play_again = 'y';
    
       srand(time(NULL));
    
       number = 1 + rand() % 1000;
    
       /* This printf should be cancelled once bug is fixed  */
       printf("%d\n", number);
    
       printf("I have a number beetween 1 and 1000\n"
              "Can you guess my number?\n"
              "Please type your first guess: ");
    
       scanf("%d", &guess);
    
       while (play_again == 'y') {
    
          while ( guessed(number, guess) == 0)
             scanf("%d", &guess);
    
          printf("Would you like to play again? (y o n)");
    
    
       /* This printf should be cancelled once bug is fixed  */
          printf("\n%c\n", play_again);
    
          scanf("%c", &play_again);
       }
    
       return 0;
    }
    
    
    
    int guessed(int real_number, int player_guess)
    {/*******************************************/
    
       if ( player_guess < real_number)
          printf("Too low. Try again: ");
       else if (player_guess > real_number)
          printf("Too high. Try again: ");
       else
          printf("Excellent! You guessed the number\n");
    
       return  real_number == player_guess ;
    
    }
    Thank you...

  2. #2
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    The scanf("&#37;c") is consuming the newline which remains in the buffer after the scanf("%d"), rather than skipping whitespace like it usually does. So instead of 'y', you get '\n' and the program quits.

    You have to either empty the input buffer up to and including the next newline, or use a different formatter to read the response. I'd suggest %1s instead.

    This is the classic problem of newline left sitting in the buffer after scanf().

    EDIT: In code:

    Code:
    char response[2];
    
    printf("Wanna play again?\n");
    scanf("%1s", response);
    if(response[0] == 'y')
    {
        /* User answered yes... */
    }
    EDIT EDIT: A better way. Tell scanf() to skip whitespace before reading the char:

    Code:
    scanf(" %c", &play_again);
    Notice the SPACE before %c
    Last edited by brewbuck; 11-16-2007 at 03:08 PM.

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,662
    Or use fgets() for every input, then extract what you need.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

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. Help with a basic scanf procedure.
    By killpoppop in forum C Programming
    Replies: 9
    Last Post: 11-03-2008, 04:39 PM
  3. Replies: 2
    Last Post: 02-20-2005, 01: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