GIDForums  

Go Back   GIDForums > Computer Programming Forums > C++ Forum
User Name
Password
Register FAQ Members List Calendar Search Today's Posts Mark Forums Read

 
 
Thread Tools Search this Thread Rate Thread
  #1  
Old 01-Dec-2005, 13:05
nikp nikp is offline
Invalid Email Address
 
Join Date: Nov 2005
Posts: 7
nikp is on a distinguished road

Need help with getline. Unable to insert strings.


Hello everyone,

I wrote a small program in order to find out how getline works but out of nowhere, I ended up trying to understand what happens and it is not working properly. Till now I found out nothing. Could you please help me?

Below is the code.

CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main() {
	int a, b;
	string s1, s2;

	for(int i = 0; i < 4; i++) {
		cout << "a: ";
		cin >> a;
		
		cout << "s1: ";
		getline(cin, s1);
		
		cout << "b: ";
		cin >> b;
		
		cout << "s2: ";
		getline(cin, s2);
	}

	return 0;
}
And here is what happens when I run the program.

a: (waits value, I insert 4)
s1: b: (waits value)
Why does it not request from the user to insert a value for s1??
It goes directly to b:.

The same happens with b and s2 until the loop ends.

Any ideas?
Thank you
  #2  
Old 01-Dec-2005, 14:44
Sokar Sokar is offline
Member
 
Join Date: May 2005
Posts: 243
Sokar has a spectacular aura aboutSokar has a spectacular aura about

Re: Need help with getline. Unable to insert strings.


It is because you are mixing cin and getline. Long story short cin leaves the '\n' in the stream and this is the delimiter for getline so getline finds the '\n' and stops reading. Just add cin.ignore();(ignore will skip 1 character with no arguments) before getline.
CPP / C++ / C Code:
#include <iostream>
#include <string>

using namespace std;

int main() {
	int a, b;
	string s1, s2;

	for(int i = 0; i < 4; i++) 
	{
		cout << "a: ";
		cin >> a;
		
		cout << "s1: ";
		cin.ignore();
		getline(cin, s1);
		
		cout << "b: ";
		cin >> b;
		
		cout << "s2: ";
		cin.ignore();
		getline(cin, s2);
	}

	return 0;
}
  #3  
Old 01-Dec-2005, 17:51
WaltP's Avatar
WaltP WaltP is offline
Outstanding Member
 
Join Date: Feb 2004
Location: Midwest US
Posts: 3,335
WaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to allWaltP is a name known to all

Re: Need help with getline. Unable to insert strings.


To add little info to Sokar's excellent description:

CPP / C++ / C Code:
cout << "a: ";
cin >> a;
Enter: 56<enter> -- 3 characters in buffer.
cin: 5 & 6 read, <enter> is not a digit
Buffer: <enter>

CPP / C++ / C Code:
cout << "s1: ";
getline(cin, s1);
Enter: nothing (<enter> is still in buffer).
cin: <enter> read
Buffer: empty

CPP / C++ / C Code:
cout << "b: ";
cin >> b;
Enter: 129<enter> -- 4 characters in buffer.
cin: 1 & 2 & 9 read, <enter> is not a digit
Buffer: <enter>

CPP / C++ / C Code:
cout << "s2: ";
getline(cin, s2);
Enter: nothing
cin: <enter> read
Buffer: empty
__________________

During the election they said Obama could only be elected when pigs fly. Well, we currently have an epidemic of Swine Flu. Coincidence?
 
 

Recent GIDBlogOnce again, no time for hobbies by crystalattice

Thread Tools Search this Thread
Search this Thread:

Advanced Search
Rate This Thread
Rate This Thread:

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
Strings tripping me up once more Elsydeon C++ Forum 5 04-Dec-2005 18:41
need help - questions about strings Benayoun C Programming Language 6 24-Jan-2005 03:15
array of pointers to strings mirizar C++ Forum 5 21-Jan-2005 11:24
I am reviewing Arrays and need help converting some strings to arrays jenmaz C Programming Language 22 23-Nov-2004 00:26
urgent help needed :c + mysql insert jack C Programming Language 1 13-Apr-2004 22:16

Network Sites: GIDNetwork · GIDWebHosts · GIDSearch · Learning Journal by J de Silva, The

All times are GMT -6. The time now is 13:05.


vBulletin, Copyright © 2000 - 2009, Jelsoft Enterprises Ltd.