cin.ignore() in C

This is a discussion on cin.ignore() in C within the C Programming forums, part of the General Programming Boards category; Silly question but I cannot find the answer online.. Is their a C function that does the same as cin.ignore() ...

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

    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
    I'm just trying to be a better person - My Name Is Earl

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    22,304
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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
    Captain Crash brewbuck's Avatar
    Join Date
    Mar 2007
    Location
    Portland, OR
    Posts
    7,274
    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,682
    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,053
    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, 05: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, 09: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, 02:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM

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