Why I have to cin.ignore() ?

This is a discussion on Why I have to cin.ignore() ? within the C++ Programming forums, part of the General Programming Boards category; Hello there Code: #include <iostream> using namespace std; int multiply(int x, int y); int main() { int x,y; cout << ...

  1. #1
    Registered User Dr.Paneas's Avatar
    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,185
    Run your program without the ignore() functions and type "4 7" at the first prompt.

  3. #3
    Registered User Dr.Paneas's Avatar
    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,185
    Now do you see the use? (If not, do the same experiment on the "with ignore" version.)

  5. #5
    Registered User Dr.Paneas's Avatar
    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,185
    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 Dr.Paneas's Avatar
    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,185
    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 Dr.Paneas's Avatar
    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 Dr.Paneas's Avatar
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



  12. #12
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,714
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,667
    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
    Registered User manasij7479's Avatar
    Join Date
    Feb 2011
    Location
    Kolkata@India
    Posts
    2,498
    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.
    Manasij Mukherjee | gcc-4.8.2 @Arch Linux
    Slow and Steady wins the race... if and only if :
    1.None of the other participants are fast and steady.
    2.The fast and unsteady suddenly falls asleep while running !



Page 1 of 2 12 LastLast
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, 08:56 AM

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