getline() and cin.ignore()

This is a discussion on getline() and cin.ignore() within the C++ Programming forums, part of the General Programming Boards category; I have a program where I call getline(cin, something); multiple times. I need to clear the input buffer of '\n' ...

  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
    21,910
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,910
    You would use cin.ignore() after this line to ignore as many characters as necessary until the newline character:
    Code:
    cin >> loop;
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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
    21,910
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    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, 07: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, 05:59 PM

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