Public Group

Help with the Standard Input/Output Streams in C++

This topic is 3531 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

Recommended Posts

Hello, I haven't used C++ in quite awhile, but for an assignment I was asked to use it again. This program has alot of text input and output. I'm seeming to have trouble using cin to get a string with spaces in it.
string s;
getline(cin, s);

This works fine at the top of my program, but after i use cout/cin a couple times or whatever, the program just skips over these lines, not stopping and allowing user input. I'm not sure the cause of this. I've tried things like,
cout.flush();
cin.clear();
cout.clear();

right before the call to getline, but that doesn't help, program still just skips over it. Can someone give me some insight to why this happens? and possibly what I should be doing to get the desired results? Thanks, ArchG

Share on other sites
It sounds like you're doing something like:
cin >> i;getline(cin, s);

This causes problems because the extraction will leave newlines in the input buffer, which will then cause getline() to read an empty line. The easiest way to handle this is to not use the extraction operators on the stream and instead use getline() to read in lines and then parse each line separately.

1. 1
2. 2
Rutin
25
3. 3
4. 4
JoeJ
18
5. 5
gaxio
11

• 14
• 22
• 11
• 11
• 9
• Forum Statistics

• Total Topics
631765
• Total Posts
3002213
×