Thread: Skipping scanf()

  1. #1
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Unhappy Skipping scanf()

    My program keeps skipping scanf().

    The second time i want to input a value into a variable instead of prompting the user for input it automatically inputs line feed/10.

    What am i doing wrong and how can i correct it?

  2. #2
    Registered User
    Join Date
    Oct 2001
    Posts
    66
    use : fflush( stdin );
    T

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    You can read a discussion on this on the bottom half of the 1st page here:
    http://www.cprogramming.com/cboard/s...threadid=24711

    Apparently the use of fflush(stdin) is a "sin"

    Here's the answer in short:
    while(getchar() != '\n');
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by Cshot
    You can read a discussion on this on the bottom half of the 1st page here:
    http://www.cprogramming.com/cboard/s...threadid=24711

    Apparently the use of fflush(stdin) is a "sin"

    Here's the answer in short:
    while(getchar() != '\n');
    It's a "sin" because it doesn't work right. Or rather, it may work. I have one compiler where it works. I have another where it doesn't. You know why that is? Because ANSI leaves flushing of input streams "undefined". This means whoever writes the compiler can make it do whatever they want, and still can call their compiler ANSI compliant.

    For example, I could make a compiler that formatted your hard drive when you use "fflush(stdin)". And I could still call my compiler ANSI compliant, because ANSI makes no specification on what happens when you flush an input stream.

    Quzah.
    Hope is the first step on the road to disappointment.

  5. #5
    Registered User
    Join Date
    Jul 2002
    Posts
    4

    Thanx

    >Whenever you mix scanf with another input method (getchar(), >fgetc() or fgets()) you're in for some surprises.
    except that in this case i'm only using scanf

    >The safe way of getting input is to always use fgets(), then >examine the resulting buffer with sscanf - or whatever else >seems appropriate at the moment.
    I'm not supposed to have learnt fgets() or sscanf yet.

    No matter i've learnt more doing this program than in a year long course - So back to the books




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 skips lines of code in DOS using Dev-C++ and Windows XP
    By jenovanomusuko in forum C Programming
    Replies: 9
    Last Post: 12-21-2008, 03:10 AM
  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