Hi, in the above code, the line "stream >> a" seems to leave the following \n char in the stream. instead, getline() removes it, as I would expect it from cin >> too...
Is there a specuial reason, why there is this difference?
The problem showed up, when mixing "cin <<" and getline(), because I wanted to get the possibility to read in strings containing spaces, which "cin >> " can't, according to what I found.
I think this is because operator >> does (for the case of string) a copy of each char, while getline() reads the character and has a function bound to \n, so when it sees it, it skips it or goes to the next line, per se.
hm, but operator>> clears out the first "a" from the string. only the "\n" remains in the stream.
This behaviour is a little bit unintuitive (at least for me). I would expect, that the "\n" is also extracted.
Just want to know, if there is a special reason, why it is implemented like this.
btw. is there a way to check, if there is still a character available in the stream?
operator>> only removes up to what has been read. Nothing more.
It doesn't remove more because that would make it useless if you actually wanted the newline character to be left in the stream.
What would happen if the user inputed two words on the same line? Should operator>> remove the second word just because it is on the same line?
What if the user inputed some white space characters before the newline character?
operator>> shouldn't treat '\n' any different from any other whitespace character. If operator>> did remove all the white space characters after each call it would have to wait until it gets a character that is not a white space to know that it should stop. That means it can't return when you press enter because it is waiting for a non-whitespace character. You would have to type in the next input before the program would receive the previous input.