Thread: cin.ignore()??

  1. #1
    Registered User
    Join Date
    Aug 2011
    Posts
    26

    Question cin.ignore()??

    Hi, guys.I am new to c++ programming and I've got a question about the cin.ignore() function I found in a program that displays the number of digits in a number entered by the user.
    This is the code:
    Code:
    #include <iostream>
    using namespace std;
    
    int main()
    {
        unsigned int n;
        int count = n;
    while (true)
      {
     
        cout << "Please enter an integer: ";
    
      
        if (cin >> n) break;
    
      
        cin.clear();
    
        cin.ignore( 1000, '\n' );
     // I don't understand what's in the braces: 1000 and '\n' :confused:
      
        cout << " It's not correct! ";
      }
    
        cout << " You entered the number " << n << ".\n";
        while ( n > 0 )
        n /= 10,count ++ ;
        cout << "Your number has " << count << " digits.";
    
        return 0;
    }

    I really hope you can explain me that line with cin.ignore() ( I know that adding cin.ignore clears the new line from the stream but what is with 1000? )and when is the perfect time to use it and as well the while(true) loop used in the program.
    I really want to understand these things so please explain me .

    Thanks in advance !

  2. #2
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    I think I know the '\n' in the cin.ignore() but 1000?

  3. #3
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    That means it can ignore up to a 1000 characters in the stream.
    I heard that this isn't a good practice(Why...anyone?).There is a maxsize() function that can be put there which does what you want to do (Look up a reference for details).

  4. #4
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    cin.ignore(), or istream::ignore(streamsize, int), discards all the characters in the cin stream until it has discarded the amount of characters specified by the first argument(in this case, 1000), or it encounters the character specified in the second argument (in this case, '\n').

    See this article: istream::ignore - C++ Reference

    Quote Originally Posted by manasij7479 View Post
    I heard that this isn't a good practice(Why...anyone?).There is a maxsize() function that can be put there which does what you want to do (Look up a reference for details).
    It might be because the stream doesn't contain 1000 characters, so there might be issues trying to read and discard that many characters. It may be better to use cin.sync(), which effectively discards all unread characters in the stream (http://www.cplusplus.com/reference/i.../istream/sync/).
    Last edited by Ushakal; 08-13-2011 at 08:35 AM.

  5. #5
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Ushakal
    Anyway, it seems like this program probably wont do what you expect it does. I don't think (cin >> n) returns "true" if the user enters an integer...
    It is unwise to think in this haphazard manner ! [EDIT] Don't edit out pieces from your post.

    However , you(Sky_Daughter) need to initialize count to zero, not n.
    Last edited by manasij7479; 08-13-2011 at 08:39 AM.

  6. #6
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Ushakal View Post
    It might be because the stream doesn't contain 1000 characters, so there might be issues trying to read and discard that many characters. It may be better to use cin.sync(), which effectively discards all unread characters in the stream (istream::sync - C++ Reference).
    I see, thanks for that information.

  7. #7
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by manasij7479 View Post
    It is unwise to think in this haphazard manner ! [EDIT] Don't edit out pieces from your post.
    Yeah, sorry, I tried to edit out before anyone read it. :P I tested it myself and it seemed to work (and you're right, of course, about initialising count to 0). I didn't realise it would work like that, since the extraction operator returns a reference the stream object itself.

  8. #8
    Registered User
    Join Date
    Aug 2011
    Posts
    26
    Thank you for replies. So should I put something else in 1000 ' s place ? A number < 1000 and the program will work,right?
    About that ( if ( cin >> n ) break; ), I never used it this way. It's the same with this : cin >> n (after the cout) break; .I 've compiled it and it works.
    The while(true)! Could you explain this to me too? I didn't meet it so often in programs.

  9. #9
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by Sky_Daughter View Post
    Thank you for replies. So should I put something else in 1000 ' s place ? A number < 1000 and the program will work,right?
    About that ( if ( cin >> n ) break; ), I never used it this way. It's the same with this : cin >> n (after the cout) break; .I 've compiled it and it works.
    The while(true)! Could you explain this to me too? I didn't meet it so often in programs.
    You can do something like:

    Code:
    cin.ignore(cin.rdbuf()->in_avail);
    rdbuf returns a pointer to the underlying stream buffer, and in_avail returns the number of characters available to read. That should discard every character in the buffer.

    while(true) is an infinite loop, which is a loop that doesn't end until interrupted by either returning from the function or using the keyword break. I think someone said it was slightly more efficient to use for( ; ; ) to make an "infinite" loop in another thread, because while(true) checks the truth value of its parameter every loop, even though it's always true. I imagine it's only a very slight performance boost though.

  10. #10
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Quote Originally Posted by Ushakal View Post
    Yeah, sorry, I tried to edit out before anyone read it. :P I tested it myself and it seemed to work (and you're right, of course, about initialising count to 0). I didn't realise it would work like that, since the extraction operator returns a reference the stream object itself.
    There's an implicit conversion from stream -> void* that happens whenever you use a stream in a bool context (such as here), which is "false" if the stream is in a fail state and "true" if the stream is in a good state.

  11. #11
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Ushakal: in_avail is a member function; you forgot the ().
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  12. #12
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    Quote Originally Posted by Ushakal View Post
    You can do something like:

    Code:
    cin.ignore(cin.rdbuf()->in_avail);
    rdbuf returns a pointer to the underlying stream buffer, and in_avail returns the number of characters available to read. That should discard every character in the buffer.
    In theory, sure. In practice, it's dicey because in_avail() isn't required to return a meaningful result. In fact, at least one popular implementation always returns 0. The most common suggestion for clearing an interactive stream buffer is:
    Code:
    #include <iostream> // For cin
    #include <ios>      // For streamsize
    #include <limits>   // For numeric_limits
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    I've delved deeply into "flushing" the input stream and posted my results here. Several common yet incorrect solutions are described.
    My best code is written with the delete key.

  13. #13
    Registered User
    Join Date
    Sep 2009
    Posts
    48
    Quote Originally Posted by tabstop View Post
    There's an implicit conversion from stream -> void* that happens whenever you use a stream in a bool context (such as here), which is "false" if the stream is in a fail state and "true" if the stream is in a good state.
    Ah, I see. Thanks for the info.

    Quote Originally Posted by Elysia View Post
    Ushakal: in_avail is a member function; you forgot the ().
    Yeah, sorry, my mistake.

    Quote Originally Posted by Prelude View Post
    In theory, sure. In practice, it's dicey because in_avail() isn't required to return a meaningful result. In fact, at least one popular implementation always returns 0. The most common suggestion for clearing an interactive stream buffer is:
    Code:
    #include <iostream> // For cin
    #include <ios>      // For streamsize
    #include <limits>   // For numeric_limits
    
    std::cin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
    I've delved deeply into "flushing" the input stream and posted my results here. Several common yet incorrect solutions are described.
    Thanks for the article; it was very informative, thanks. I only just stopped using system("PAUSE") a few days ago myself, after I learned how bad that was in another thread.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore();
    By BlackSlash12 in forum C++ Programming
    Replies: 7
    Last Post: 08-24-2007, 02:20 PM
  2. cin.ignore() in C
    By swgh in forum C Programming
    Replies: 10
    Last Post: 08-09-2007, 10:45 PM
  3. 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
  4. ignore()
    By Supar in forum C++ Programming
    Replies: 7
    Last Post: 01-06-2003, 02:11 PM
  5. ignore
    By indigo0086 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2002, 09:56 AM