Thread: cin.ignore()

  1. #1
    Registered User
    Join Date
    Jul 2013
    Posts
    18

    cin.ignore()

    Hello, I am sorry to clutter this board which obviously has bigger problems than this with such a simple and possibly stupid question, but I am one of those people that cannot tolerate simply "accepting" that "that's how it works." If I don't understand it, I just can't live with it. So here goes.

    This is code from the Intro to C++ tutorial:

    Code:
    #include <iostream>
     
    using namespace std;
     
    int main()
    {
      int thisisanumber;
     
      cout<<"Please enter a number: ";
      cin>> thisisanumber;
      cin.ignore();
      cout<<"You entered: "<< thisisanumber <<"\n";
      cin.get();
    }
    It says on the page that cin.ignore() throws out the Return key input, presumably leaving only the numerical data.

    However, what I don't understand is this: If the cin>> function has already executed, then the enter key would presumably already have been read into the variable thisisanumber. That being the case, how does cin.ignore() know that it's throwing out the last input char on that variable without being fed the variable name?

    I could understand if they executed simultaneously, but I'm assuming execution is line-by-line. You reach cin>>, and then the program is waiting for the input, fed in by, and including, the enter key. So now this enter key input is sitting in the variable. Then we execute "cin.ignore()" - but ignore what? How does it know it's ignoring that enter key in that variable?

    Sorry for explaining twice but I wanted to make sure it was apparent what I was asking.

    Thanks a lot,
    -summit45

  2. #2
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    When cin is reading a number it stops when it finds a non-number.
    The newline character is a non-number, so cin stops "before" reading it
    (actually, it reads it and pushes it back).
    So the newline character is still in the stream and will be the character that cin.ignore() ignores.
    Unless of course you entered something like 123abc (try it).

  3. #3
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,817
    Input in C/C++ is line buffered. When you write cin>>thisisanumber in your program and execute it, the code waits for the newline character before the data in the input stream is processed. Before that newline character comes along it just sits there waiting. Processing of the user input number stops when it's determined that the remaining characters in the input stream cannot be converted into an integer. In your case it would most likely be the newline character but it could be some other character such as if you had entered "12345abcdef" followed by the newline. In that example, the "1235" would be extracted from the stream. The newline does not sit in the variable, it stays on the input stream waiting to be handled in some fashion by subsequent input operations. So, the ignore call in your case doesn't really know what it is being asked to ignore, just that there is something that needs to be ignored.

    In most cases, the single ignore statement would work by consuming the stray newline character. Then, later in the program when you have your cin.get() call, that function will block (wait) for input since there is nothing in the input stream at that point.

    In the example I have here however, the extra characters would cause a problem. The single ingore call would be insufficient as it would only consume one (of many) characters still left in the input stream. When the program reached the cin.get() function, the function would immediately consume a character and then the program would exit. Depending on how the program was run, this could mean that the user would not get to see the full output before the program window closed. In that case, you'd want to ignore more than just a single character and there are arguments to the cin.ignore function that enable that which basically say "throw out whatever might be left in the input stream no matter how many characters there might be".
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  4. #4
    Registered User
    Join Date
    Jul 2013
    Posts
    18
    Wow, thank you both, that's very, very clear. I understand perfectly now lol.

    Yeah, I've looked a bit at C and C++ in the past, but now I'm coming back to it because I want to learn some programming. People encouraged me to learn Java but my (admittedly limited but to me, sufficient) experiences with it already strike me as it being sloppy and frankly pathetic. So I decided I'd come back here and learn C++ because it's a more legitimate language in my view.

    -summit45

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. cin.ignore and cin.get in C
    By Trafalgar Law in forum C Programming
    Replies: 5
    Last Post: 11-24-2008, 10:34 AM
  2. cin.ignore()
    By valthyx in forum C++ Programming
    Replies: 2
    Last Post: 07-22-2008, 04:23 AM
  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. cin.ignore() & cin.get()
    By dragonlady in forum C++ Programming
    Replies: 11
    Last Post: 08-08-2005, 11:38 PM
  5. ignore()
    By Supar in forum C++ Programming
    Replies: 7
    Last Post: 01-06-2003, 02:11 PM