left over buffer characters

This is a discussion on left over buffer characters within the C Programming forums, part of the General Programming Boards category; hey brainiacks!! I need a little help if somebody has got five minutes. i wana know how and why i ...

  1. #1
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51

    Wink left over buffer characters

    hey brainiacks!!

    I need a little help if somebody has got five minutes.

    i wana know how and why i sometimes get characters left
    over in the buffer, resulting in a getchar() (i'm learning c)
    not working. I've read a few articles and the 'how to' on pausing
    the screen in FAQ's but nothing has answeared my question.

    If you could give a code example as well id be both greatful
    and enlightened.

  2. #2
    Gawking at stupidity
    Join Date
    Jul 2004
    Location
    Oregon, USA
    Posts
    3,159
    People usually have this problem when using scanf(). scanf() only processes the buffer as far as you tell it to the in the format string. If the format string is "%d" and the user types 123abc it will leave "abc\n" in the buffer.

    To avoid leftover characters use fgets() to retrieve all input and then you can use sscanf() on the input to get what you want.
    If you understand what you're doing, you're not learning anything.

  3. #3
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,532
    Most likely, you are just using a bad conbination of scanf and getchar, and need to read some FAQ. If that is the case,
    http://c-faq.com/stdio/scanfinterlace.html
    http://c-faq.com/stdio/scanfhang.html

    Many times in input routines the terminal will pause and give time for the user to type, and usually the user presses enter when he finishes.

    Depending on how you have your program set up, and the kind of data you are asking for, it is always easy for the user to type more than you might need. The extra data does not disappear, it stays in the input buffer. Even if the user is responsible and types exactly what you need many input functions use Enter as a signal to stop recording, and there is an extra newline in the buffer. When your function returns this extra data is still there, it does not (and maybe shouldn't try) to clean up after itself.

    So programmers have to come up with a few nifty solutions to try and "flush" the input buffer. Lucky people have an implementation of fflush(stdin); but that is not guaranteed by Standard C, and is not portable. Many people just choose to read input differently, perhaps by roll their own input functions where all the input is stored at some point, completely avoiding the headache. Some just clean it up with getchar() in a loop.

  4. #4
    Registered User the bassinvader's Avatar
    Join Date
    Jul 2006
    Location
    Europe
    Posts
    51
    thanks for the answears.

    they were really helpful.

    I especially liked ' itsme86 ' caption...." If you understand what you're doing, you're not learning anything."

    very true!

  5. #5
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,046
    Keep looking through the FAQ, you'd probably have found something that would have helped eventually . . . like this one: http://faq.cprogramming.com/cgi-bin/...&id=1043284392
    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.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Why only 32x32? (OpenGL) [Please help]
    By Queatrix in forum Game Programming
    Replies: 2
    Last Post: 01-23-2006, 01:39 PM
  2. Replies: 15
    Last Post: 10-31-2005, 07:29 AM
  3. Replies: 4
    Last Post: 09-23-2005, 10:51 AM
  4. pointer to array of objects of struct
    By undisputed007 in forum C++ Programming
    Replies: 12
    Last Post: 03-02-2004, 03:49 AM
  5. mygets
    By Dave_Sinkula in forum C Programming
    Replies: 6
    Last Post: 03-23-2003, 06:23 PM

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