• Advertisement
Sign in to follow this  

std::cin operator

This topic is 4727 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

the code: std::cin >> c; requires to type a char then press enter. in VC2003.net it looks like I should use std::cin >> c >> c2; to hold the enter key in c2. Anybody knows whats wrong? I have like: std::cin >> c; // do something std::cin >> c; the second time was skipped everytime... Using msvc 2003 .net - In debug mode, it happens correctly that operator >> ignores the white spaces, but in release mode it freaks out... can anybody confirm?

Share this post


Link to post
Share on other sites
Advertisement
First and foremost, std::cin isn't an operator, it's a global variable. [grin] Now, a carriage return is a perfectly valid character, which isn't discarded when you just do character-by-character input. So if you type a character and then return, you've indeed got to get rid of both the character and the newline.

Have a look at the std::ws manipulator, or at std::cin's ignore member function. In particular, std::cin.ignore(std::numeric_limits<int>::max(), '\n'); will ignore all the characters up to and including the first newline encountered (i.e. it will discard the remainder of the line and eat up the newline). The std::numeric_limits trait template class can be found in <limits>

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement