Thread: cin.ignore() in C

  1. #1
    Its hard... But im here swgh's Avatar
    Join Date
    Apr 2005
    Location
    England
    Posts
    1,688

    cin.ignore() in C

    Silly question but I cannot find the answer online..

    Is their a C function that does the same as cin.ignore() in C++?

    Sombody said it was fflish( stdlin ) but iv heard thats bad...

    any info appreicated
    Double Helix STL

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Considering that one can actually use cin.get() instead of cin.ignore() in C++ and just ignore what is returned, I would think that one can do the same in C, but with getchar() instead.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    If you want it to wait until the user hits enter I do:
    Code:
    while(getchar() != '\n');

  4. #4
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by mike_g View Post
    If you want it to wait until the user hits enter I do:
    Code:
    while(getchar() != '\n');
    I highly recommend that when coding an empty loop, the semicolon should go on its own line, perhaps with a comment:

    Code:
    while(getchar() != '\n')
        ; /* Empty loop */
    This makes it clear that you did it intentionally.

  5. #5
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    check for EOF.
    Code:
    while((getchar() != '\n') && !feof(stdin))
        ; /* Empty loop */

  6. #6
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    check for EOF.
    Code:
    while((getchar() != '\n') && !feof(stdin))
        ; /* Empty loop */
    Because of the nature of this particular loop, calling feof() like this is okay, but not in general... Don't use this as a general pattern for a read loop.

  7. #7
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    I don't understand, what's bad about the pattern?

  8. #8
    Officially An Architect brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,396
    Quote Originally Posted by robwhit View Post
    I don't understand, what's bad about the pattern?
    Suppose you've just read the last char in the file. You are at the end-of-file. You call feof() and it returns... false! feof() will not return true until a read operation has FAILED while at the end of file. Result? You pass through the loop one time too many.

    As I said, in THIS case it doesn't matter, because the loop is empty. In general, controlling an input loop with feof() is not correct.

  9. #9
    Registered User ssharish2005's Avatar
    Join Date
    Sep 2005
    Location
    Cambridge, UK
    Posts
    1,732
    And there is a details description of why feof shouldn't be used as well in the FAQ. Have a go with the FAQ once.

    ssharish2005

  10. #10
    Registered User
    Join Date
    Apr 2006
    Posts
    2,149
    No there isn't. (at least not that I'm aware of)

    Using getchar() will not work if there is nothing to clear in the buffer -- it will force you to press return an extra time.
    It is too clear and so it is hard to see.
    A dunce once searched for fire with a lighted lantern.
    Had he known what fire was,
    He could have cooked his rice much sooner.

  11. #11
    Registered User
    Join Date
    Oct 2001
    Posts
    2,129
    Quote Originally Posted by brewbuck View Post
    Suppose you've just read the last char in the file. You are at the end-of-file. You call feof() and it returns... false! feof() will not return true until a read operation has FAILED while at the end of file. Result? You pass through the loop one time too many.

    As I said, in THIS case it doesn't matter, because the loop is empty. In general, controlling an input loop with feof() is not correct.
    this only applies if you have more input in the body and you don't check for EOF, which isn't a problem relating to using feof in the conditional part of the loop, unless it's the only expression in the code.

    It isn't bad, but it can be used wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Console window waiting on cin.ignore() or cin.ignore(2)
    By The SharK in forum C++ Programming
    Replies: 3
    Last Post: 07-19-2006, 04:17 PM
  2. Can't seem to come to conclusion : cin.ignore()
    By SkyHi in forum C++ Programming
    Replies: 8
    Last Post: 05-13-2005, 08:57 PM
  3. Why do we use cin.ignore()
    By himanch in forum C++ Programming
    Replies: 2
    Last Post: 02-04-2005, 01:52 PM
  4. need help with cin.get, or cin.ignore
    By yoyo in forum C++ Programming
    Replies: 5
    Last Post: 09-23-2003, 01:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM