Thread: scanf is screwed-up

  1. #1
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584

    scanf is screwed-up

    I was just programming and I normally use fgets to get a string, but then that was acting weird. It was holding something in the buffer and then it would ruin the next "user choice".

    Well, I think I know why it did that. Was it because the buffer was not cleared? How would you clear that with fflush?

    Well anyway, then I tried scanf. That allows me to put the one word with no spaces (hence "%s") and if I did put a second word, it would hold it in the buffer and also screw up the next "user choice".

    Arg!!! How do you flush the buffer? Thanks.

  2. #2
    Banned Troll_King's Avatar
    Join Date
    Oct 2001
    Posts
    1,784
    To clear the standard input buffer you can use:
    while(getchar() != '\n') continue;

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > but then that was acting weird.
    Probably because you used a scanf elsewhere.

    The 'scanf leaves newlines' problem is a disaster area for fgets.

    If you always stick to fgets for reading input, you shouldn't have a problem.
    But if you do, you'd better post the code here.
    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.

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yes, I did use scanf twice. Bad idea, right? I'll post the code later. I don't have the .c file with me now. Maybe tonight.

    I don't think I really understand what the problem is. But, I'll post the code and you can better explain it. Thanks.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    24

    I have scanf problems too.

    I use the compiler jgrasp, and scanf presents problems for me too.

    for example, my code will be

    Code:
    printf ("Enter the value for N: ");
    scan f ("%lf", &N);
    Instead of printing "Enter the value for N: " and scanning, it scans and THEN prints "Enter the value for N: ". I've gotten around this problem by just putting a line break \n after my printfs, but sometimes I wish I could scan for text on the same line! Has anyone ever encountered this problem, because my prof in class will enter the exact same lines of text, compile, and have it come out working perfectly!

  6. #6
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Sorry, I'm still not at the computer with the files. But I have some more questions. Why is scanf holding words in the buffer just to later screw up other inputs? And, when I type in "Garfield the cat", the string is only "Garfield" and it caries over "the cat". Why? I'll post the code soon. Thanks.

    --Garfield

  7. #7
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    > printf ("Enter the value for N: ");
    > scanf ("%lf", &N);

    To ensure you get the prompt, do this.

      printf ("Enter the value for N: ");
      fflush( stdout );
      scanf ("%lf", &N);

    This is the correct use of fflush - its for output streams (all those who think fflush(stdin) is a good idea take note).

    > And, when I type in "Garfield the cat", the string is only "Garfield" and it caries over "the cat". Why?
    Probably because you're using %s
    %s only reads words - it stops at the first white space/newline
    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.

  8. #8
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Okay, I should have know the "%s" because, well, I do from "%d %d" takes two integers separated with a space. Okay, I'll give the fflush(stdout) a try because I think that's why fgets was getting screwed up, which made me revert to scanf (bad idea, I know). Thanks.

    --Garfield

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. scanf issue
    By fkheng in forum C Programming
    Replies: 6
    Last Post: 06-20-2003, 07:28 AM
  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