C Board  

Go Back   C Board > General Programming Boards > C++ Programming

Reply
 
LinkBack Thread Tools Display Modes
Old 06-27-2009, 11:29 AM   #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?
swappo is offline   Reply With Quote
Old 06-27-2009, 11:31 AM   #2
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 06-27-2009, 11:42 AM   #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?
swappo is offline   Reply With Quote
Old 06-27-2009, 11:44 AM   #4
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 06-27-2009, 11:53 AM   #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().
swappo is offline   Reply With Quote
Old 06-27-2009, 11:58 AM   #6
C++ Witch
 
laserlight's Avatar
 
Join Date: Oct 2003
Location: Singapore
Posts: 10,352
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
Build + Version Control System: SCons + Bazaar

Look up a C/C++ Reference and learn How To Ask Questions The Smart Way
laserlight is offline   Reply With Quote
Old 06-27-2009, 12:12 PM   #7
Registered User
 
Join Date: Nov 2008
Posts: 56
Ok, thanks.
swappo is offline   Reply With Quote
Reply

Thread Tools
Display Modes

Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Console window waiting on cin.ignore() or cin.ignore(2) The SharK C++ Programming 3 07-19-2006 04:17 PM
getline problem Nick_365 C++ Programming 1 03-10-2006 08:36 PM
getline problem Bitphire C++ Programming 5 10-18-2004 04:42 PM
need help with cin.get, or cin.ignore yoyo C++ Programming 5 09-23-2003 01:14 AM
getline run-on twiz C++ Programming 4 03-02-2003 06:59 PM


All times are GMT -6. The time now is 07:26 PM.


Powered by vBulletin® Version 3.8.1
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Optimization by vBSEO 3.3.0 RC2

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