Jump to content
  • Advertisement
Sign in to follow this  
ArchG

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.

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
Advertisement
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
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!