Thread: getline() and cin.ignore()

  1. #1
    Registered User
    Join Date
    Nov 2008
    Posts
    56

    getline() and cin.ignore()

    I have a program where I call getline(cin, something); multiple times. I need to clear the input buffer of '\n' but if I use cin.ignore() after getline(), I get a cursor on the command line and have to hit enter before being prompted for the next input. How do I clear the input buffer with out this extra line?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swappo
    I have a program where I call getline(cin, something); multiple times. I need to clear the input buffer of '\n'
    The newline is also consumed by the getline call, so you do not need to get rid of it.
    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 2008
    Posts
    56
    Example:
    Code:
    char loop = 'y';
    	while(loop != 'n')
    	{
    		cout << "Enter a name: ";
    		getline(cin, name);
    		cout << "Enter a number: ";
    		getline(cin, num);
    		numbers[name] = num;
    		cout << "Enter another name and number? 'y' or 'n': ";
    		cin >> loop;
    	}
    Here is my output without flushing the buffer:
    Code:
    Enter a name: me
    Enter a number: 45
    Enter another name and number? 'y' or 'n': y
    Enter a name: Enter a number:
    How would I fix this?

  4. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    You would use cin.ignore() after this line to ignore as many characters as necessary until the newline character:
    Code:
    cin >> loop;
    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

  5. #5
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    If I put cin.ignore() after getline(cin, name); I have a line with just a cursor which I have to hit enter to get to the next line of input. Yet, I put cin.ignore() after cin >> loop; as you suggested it works without that extra line. I don't understand why I get differing results with the different placements of cin.ignore().

  6. #6
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Quote Originally Posted by swappo
    If I put cin.ignore() after getline(cin, name); I have a line with just a cursor which I have to hit enter to get to the next line of input. Yet, I put cin.ignore() after cin >> loop; as you suggested it works without that extra line. I don't understand why I get differing results with the different placements of cin.ignore().
    As I noted, getline would consume the newline. Thus, if there is no newline to ignore, you end up waiting for more input, against your intention. On the other hand, formatted input with operator>> does not consume the newline (by default, if I remember correctly), thus there is (at least) a newline left in the input buffer. If this newline is not ignored, the next call to getline will consume it, against your intention.
    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

  7. #7
    Registered User
    Join Date
    Nov 2008
    Posts
    56
    Ok, thanks.

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. getline problem
    By Nick_365 in forum C++ Programming
    Replies: 1
    Last Post: 03-10-2006, 08:36 PM
  3. getline problem
    By Bitphire in forum C++ Programming
    Replies: 5
    Last Post: 10-18-2004, 04:42 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. getline run-on
    By twiz in forum C++ Programming
    Replies: 4
    Last Post: 03-02-2003, 06:59 PM