Thread: ignore after getline

  1. #1
    Registered User
    Join Date
    Nov 2018
    Posts
    30

    ignore after getline

    Hi all,

    after googling a bit, i found out, that ignore after getline is not necessary, however, i had the following code:
    Code:
    cout << "Enter your name: ";
    getline(cin, name);
    // cin.ignore();
    cout << "Enter your age: ";
    cin >> age;
    if i keep the "cin.ignore(), and after entering a name and pressing ENTER, i don't get the "Enter your age" message until i press ENTER again. W
    WHat happens there exactly? why is execution is stopped there, like it's waiting for an ENTER?

    thanks in advance

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,082
    You're reading an entire line, then you're reading more input to ignore it, before you print the next prompt.

    So when the user enters their name, you're still waiting for them to enter something else so you can discard it before you prompt them for their age.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Nov 2018
    Posts
    30
    understood, so i cannot ignore "Nothing", the program needs something to ignore
    thanks a lot

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 3
    Last Post: 12-13-2011, 07:32 AM
  2. getline() and cin.ignore()
    By swappo in forum C++ Programming
    Replies: 6
    Last Post: 06-27-2009, 12:12 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. difference between getline and cin.getline
    By bartinla in forum C++ Programming
    Replies: 3
    Last Post: 11-13-2004, 09:47 AM
  5. getline and ignore
    By Kyoto Oshiro in forum C++ Programming
    Replies: 3
    Last Post: 03-01-2002, 07:03 PM

Tags for this Thread