Thread: New to C, Scanf being ignored

  1. #1
    Registered User
    Join Date
    Jan 2009

    New to C, Scanf being ignored

    Hi I am new to C and have to do an assignment for a class. I have a rough idea how I am going to do it, but the program isn't behaving the way I want it to.

    I ask the user to enter a number using scanf

    int user = 1; // User Defined Number, initialized to 1
    char answer = ' '; // The player's answer
    int computer = 50;

    // If the User does not enter a digit, value is 1 by default
    printf("Please select a number between 1 and 100\n");
    scanf("%d", &user);

    then the computer asks if their number is above below or equal to another number:
    printf("Is your number smaller than, equal to, or greater than %d? (enter <, =, > respectfully)", computer);
    scanf("%c", &answer);

    When I run the program, it ends before the second scanf happens.....I don't understand why.

    I looked on google and someone suggested flushall(); I don't know what this does.

    Any help would be appreciated

  2. #2
    Join Date
    Jan 2009
    That is a common issue with scanf(). It basically takes control of your input buffer, so if you mix it with other functions fun stuff occurs. Can you give a code sample? Or maybe its easiest to say "use fgets() and sscanf()."

  3. #3
    Registered User
    Join Date
    Jan 2009
    We just started learning C and I don't know what fgets() or sscanf() does, we are supposed to use scanf and printf as far as I know....

    and I basically gave code samples. Thanks for the reply though.

    Long story short this is a program that tries to guess the users number, and I want to keep asking them to enter <, > or =, so I will use a while loop to ask them, but first I want to be able to ask at least ONCE, which isn't working.

  4. #4
    Registered User
    Join Date
    Sep 2006
    Undoubtedly THE most frequently asked question.

    The answer is that scanf() reads up to the first newline char '\n' it encounters in the input stream, or the first char it can't format as it expects.

    Way to deal with this is:

    scanf("%c", &mychar);
    //now, pull the newline char (which you get every time you hit enter key)
    //off of the keyboard buffer, with:
    getchar();  //pulls one char off the standard input stream
    //for Turbo C, use
    someVariable = getchar(); someVariable++; //can be an int or a char, either one. 
    //which stops stupid warnings from Turbo C
    //Now scanf() is ready to stop ignoring you.
    You need to use getchar() after every scanf(), to keep the input buffer clear. Note that
    fflush(stdin), is undefined - sometimes it works, sometimes it doesn't. fflush() is designed for use on *output* buffers, not input buffers.
    Last edited by Adak; 01-27-2009 at 08:24 PM.

  5. #5
    Registered User
    Join Date
    Jan 2009
    seems to be working! thanks a lot!

    If I have any other problems I'll let you know...

  6. #6
    Join Date
    Oct 2007
    Inside my computer
    Why do you keep pulling up Turbo C? It's DEAD, and let it be.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

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 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