Sign in to follow this  

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

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
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.

Share this post


Link to post
Share on other sites

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

If you intended to correct an error in the post then please contact us.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this