Thread: Why I have to cin.ignore() ?

  1. #1
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12

    Why I have to cin.ignore() ?

    Hello there

    Code:
    #include <iostream>
    
    
    using namespace std;
    
    
    int multiply(int x, int y);
    
    
    int main()
    {
        int x,y;
        cout << "Insert the value of x: ";
        cin >> x;
        //cin.ignore();
        cout << "Insert value y: ";
        cin >> y;
        //cin.ignore();
        cout << "X multiple Y equals in " << multiply(x,y) << endl;
    
    
        return 0;
    }
    
    
    int multiply(int x, int y) {
        return (x*y);
    }
    Comment or not, cin.ignore(); does nothing in this program output. I read somewhere that we use this function to throw <Enter> away, but I don't understand what does it means... I suppose Enter represents a Carriage Return CR character or something like that, but again... I can't figure out the meaning of use the cin.ignore() since the program output is identical the same using it or not.
    Last edited by Dr.Paneas; 08-15-2011 at 08:06 AM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Run your program without the ignore() functions and type "4 7" at the first prompt.

  3. #3
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Run your program without the ignore() functions and type "4 7" at the first prompt.
    Code:
    [drpaneas@fedora Debug]$ ./cprogram1 Insert the value of x: 4 7
    Insert value y: X multiple Y equals in 28

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Now do you see the use? (If not, do the same experiment on the "with ignore" version.)

  5. #5
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Quote Originally Posted by tabstop View Post
    Now do you see the use? (If not, do the same experiment on the "with ignore" version.)
    with ignore version:

    Code:
    [drpaneas@fedora Debug]$ ./cprogram1 
    Insert the value of x: 4 7
    Insert value y: X multiple Y equals in 28
    it's the same :/

    EDIT: I suppose cin.ignore doesn't work on GNU GCC compiler ?

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    Ah, right: ignore by default only ignores one character (which in this case is the space). Something like ignore(80) may do the trick.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Nope, ignore(80) doesn't make my day
    the program hangs up and waiting until Ctrl+C is pressed manually by the user.

    From Wikipedia: "many programming languages treat carriage return and line feed as whitespace."

    So cin.ignore ignores the whitespace between 4 7 ?
    I suppose then the result would be 47 (one number) instead of 4*7=28. Hmmm ?

  8. #8
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,336
    You don't need Ctrl-C: you just need to type 80 characters. Have you, at any point, actually looked up what ignore does?

  9. #9
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Quote Originally Posted by tabstop View Post
    You don't need Ctrl-C: you just need to type 80 characters. Have you, at any point, actually looked up what ignore does?
    At last,I think I figured it out

    Extracts characters from the input sequence and discards them.
    The extraction ends when n characters have been extracted and discarded or when the character delim is found, whichever comes first. In the latter case, the delim character itself is also extracted.

    So cin.ignore(2); works for me

    Code:
    [drpaneas@fedora Debug]$ ./cprogram1 Insert the value of x: 4 7
    Insert value y: 3
    X multiple Y equals in 12
    Thanks tabstop \m/

  10. #10
    Registered User
    Join Date
    Nov 2009
    Location
    GR
    Posts
    12
    Well I made a post at my blog clarifying things pretty much
    Why do I have to use cin.ignore(); ? | Dr.Paneas Blog

  11. #11
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Dr.Paneas View Post
    Well I made a post at my blog clarifying things pretty much
    Why do I have to use cin.ignore(); ? | Dr.Paneas Blog
    Nice post.
    Also, as the post is somewhat long, you could put some other types of examples, like how to ignore a variable number of characters upto a certain one or a maximum number of them as available....etc.
    Last edited by manasij7479; 08-15-2011 at 11:40 PM.

  12. #12
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    However, you should know that it isn't possible as far as I am aware to protect against all stupidity using the cin >>. Plus such solutions are often complex.
    The safest (and best) solution is to always read in the entire line of user input, and then parse it properly to get what you need.
    If the user enters a non-integer, the stream will go into an "error state," which is extremely difficult to solve. Refer to this post for more info: "Flushing" the input stream
    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.

  13. #13
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    If the user enters a non-integer, the stream will go into an "error state," which is extremely difficult to solve.
    cin.clear();

  14. #14
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    OK, true, it does clear the error state of the stream, but not the application. The application must, after clearing the error state of the stream, get rid of unwanted left over input in the input stream, or the stream will go into an error state again.
    And clearing the left over input in the stream turns out not to be so easy.
    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.

  15. #15
    [](){}(); manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    *nullptr
    Posts
    2,657
    Quote Originally Posted by Elysia View Post
    OK, true, it does clear the error state of the stream, but not the application. The application must, after clearing the error state of the stream, get rid of unwanted left over input in the input stream, or the stream will go into an error state again.
    And clearing the left over input in the stream turns out not to be so easy.
    Well, one generally tries to cover all possible types of input during an iteration.
    So, after getting an error state and cleaning that, the data may be put into the right place by type, where it may be ignored.

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 indigo0086 in forum C++ Programming
    Replies: 1
    Last Post: 11-13-2002, 09:56 AM