Thread: cin.ignore();

  1. #1
    Registered User
    Join Date
    Aug 2007
    Posts
    66

    Post cin.ignore();

    I am a begginner in C++ and in programming generally. I am reading http://www.cprogramming.com/tutorial/lesson1.html this tutorial and I have one question.

    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();
    }
    I could not understand the purpose of the cin.ignore() function. I recompile the module without the cin.ignore() function and the result was the same.

    While running the program, I typed a character instead of a number and the result was the number 2. eg. I typed "s" and the output was "2". I typed 43243432767436482 and the output was 2 again. So what the cin.ignore() really does ?

  2. #2
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    I believe cin.ignore() just reads a character from stdin and discards it. In this case, it's probably throwing away the '\n' that you are entering when you press Enter, assuming you enter a legit int.

    If you are not entering a legit int, then you're messing things up. Don't expect the output to make sense.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    When mixing cin >> and getline, the extra newline causes problems. One solution is to always call cin.ignore() after calling cin >> because it doesn't cause any problems when you don't use getline or get afterwards.

    In this case, get() is called later, so it does matter. The purpose of the cin.get() at the end of the program is to force the user to respond before the program finishes (so that the output can be read before the console window closes on some platforms). If you don't call cin.ignore(), then the newline in the input stream after the cin>> thisisanumber call will be read in by cin.get(). Then the window won't stay open and the user won't be able to read the output.

    If you're console window stays open automatically, then you don't have to worry about it in this instance, but others do so that is added.


    Also, if you initialize the variable to some number, then input s or 43243432767436482, then chances are the output would be whatever number you initialized it to. In this case the random value in memory it gets when uninitialized is probably 2.

  4. #4
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Daved, what is "getline" ?

    Instead of using cin.get() , may I use system("PAUSE"); ?

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    getline is a function that reads an entire line of text into a string (among other things). It is similar to get, which reads a single character instead of a string of characters.

    Instead of cin.get(), you could use system("PAUSE"), but there's no real reason to. The code in the original example will work everywhere, and there's nothing wrong with leaving the cin.ignore() in the code.

  6. #6
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    Well after experimenting with these I found out and solve my query.
    When I use cin.ignore() in the code the MS-DOS Window does not dissappear immediately, even with cin.get() at the bottom of the code. So, cin.ignore() in combination with cin.get() is a "must" to make MS-DOS problems run away. Although, I discover that using system("PAUSE") instead of both cin.get() and cin.ignore, it does the same thing.

    So, as you said previously, there are no errors. Have a nice day :P
    Thanks a lot.

  7. #7
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Note that the cin.ignore() is only a "must" with the cin.get() if you use cin >> previously in the program.

  8. #8
    Registered User
    Join Date
    Aug 2007
    Posts
    66
    I got that master. Good point worth mentioning.

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, 04: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, 08: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, 01:14 AM
  5. cin.ignore
    By Honderman in forum C++ Programming
    Replies: 2
    Last Post: 01-08-2002, 09:51 PM