Good writeup that explains some of the reasons why you are running into this issue, primarily due to the behavior of the input types and that you are mixing them.
Up vote 5 down vote favorite 1 share g+ share fb share tw.
Here is the code: string str; cin>>str; cout>str", it seems '\n' is still stored in cin (using cin.peek() to check), which ends getline immediately. The solution will be adding one more line between the first usage and the second one: cin. Ignore(numeric_limits::max(), '\n'); However, I still don't understand, why is '\n' left there after the first call?
What does istream& operator>> really do? C++ cin link|improve this question asked Mar 26 '10 at 17:30gc .117310 100% accept rate.
1 Some more digging reveals how subtle the description can be: "Extraction ends when the next character is either a valid whitespace or a null character, or if the End-Of-File is reached. " – gc . Mar 26 '10 at 20:36.
The \n is left in the input stream as per the way operator>> is defined for std::string. The std::string is filled with characters from the input stream until a whitespace character is found (in this case \n), at which point the filling stops and the whitespace is now the next character in the input stream. You can also remove the \n by calling cin.get() immediately after cin>>str.
There are many, many different ways of skinning this particular I/O cat, however. (Perhaps a good question in and of itself? ).
4 A better way of getting rid of trailing whitespace characters would probably be to do std::cin >> str >> std::ws;. – sbi Mar 26 '10 at 17:47 1 Run some tests, realizing it is not the problem of how it is defined for std::string. In fact, operator>> leaves the newline in the stream whatever variable is following it.
But thanks for starting the discussion of getting rid of white space! – gc . Mar 26 '10 at 19:47.
By default, the stream insertion operator reads until it sees whitespace. Your first call isn't returning until it sees a space, tab, newline, etc. Then the next character needs to be consumed so that you can get to the next one.
I generally recommend only doing line-oriented input from std::cin. So, your code could look something like this: string str; int val; // Read an entire line and parse an integer from it { string line; getline(cin, line); istringstream iss(line); iss >> val; } cout>, the input does not terminate if the user hits enter instead of inputting what is required, but instead continues until a token is input (this behavior is usually not wanted).
As others have said, th problem is that the newline is left from the first extraction. One solution I do is to discard all the left characters in the stream: std::cin. Ignore( std::numeric_limits::max() ).
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.