“getline" and "cin" conflicts

Hi I just wonder why can't the program run as it is supposed to.
Could anyone explain how it really work? THX!

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <string>

using namespace std;
int main() {
	string x;
	cin >> x;
	
	
	string s;
	getline(cin,s);
	
	cout << x << endl;
	cout << s << endl;
	return 0;
}
This does exactly what it's supposed to do, but it may not be what you expect. What do you expect, and what happens?
closed account (DSLq5Di1)
I gather the output of s is always blank? the extraction operator >> ignores whitespace characters but getline() does not. A new line character remains in the stream after your first input.

Use cin.sync() before getline() to clear the input stream.
cin.sync() isn't required to do anything useful.

Use cin.ignore( numeric_limits <streamsize> ::max(), '\n' ); after cin >> foo;.

http://www.cplusplus.com/forum/beginner/20577/#msg107828
http://www.cplusplus.com/forum/beginner/22128/#msg116074

Hope this helps.
Thank you guys! that's helpful. I'm just a beginner so I don't quite understand how those operators work.
actually someone told me if I add
1
2
3
char newline_character;
newline_character = cin.get()



before the get line, it works normally.

but how does cin.get() work?
Well Im new too, but when looking at your code, The first thing I noticed was that if that was to run.

Your program will run, then go off before you got to see it work. you would need a
system ("pause");

before the return to get it to stop,

as in for the difference between the two, for output, on a monitor, nothing as far I noticed, but when putting output to a file eg ".txt." for fstreams

then you will need getline to identify which string variable your dealing with.
Ssturges wrote:
Your program will run, then go off before you got to see it work. you would need a
system ("pause");

before the return to get it to stop,

That is not true, please do not advocate using system("PAUSE");.
See http://www.cplusplus.com/forum/articles/7312/ for more.

Also, the output should not vary if you change the standard streams to file(s). If it does, your compiler is broken.
Side Note: like I said, Im new to this as well, just to make sure I understand what I read, its bad to use System "Pause"; because its only a windows related problem?.

Im sorry I just want to make sure I know why its bad, and what would be a better solution to that problem?
closed account (DSLq5Di1)
Duoas wrote:
cin.sync() isn't required to do anything useful.

Could you explain what you mean by this? and link to any relevant articles.. I've googled around but don't see any information regarding such an issue.

-edit-

Nvm! found an informative post here:-

http://cboard.cprogramming.com/cplusplus-programming/66099-simple-cplusplus-help-beginer.html#post469412
Last edited on
According to: http://cplusplus.com/reference/iostream/istream/sync/

It actually (at least for buffered istreams) will discard unread characters. Unless I'm misreading something...
IIRC, the standard doesn't actually require sync() to do anything -- and most implementations don't.

Why system() is evil
http://www.cplusplus.com/forum/articles/11153/
Topic archived. No new replies allowed.