I am following Stroustrup's C++ PPP2 book and have a problem with understanding one of his examples. I have searched online already and also came across this question (http://www.cplusplus.com/forum/beginner/170172) but unfortunately it didn't help me.
The code for "Detect repeating word example" is:
1 2 3 4 5 6 7 8 9 10 11
int main()
{
string previous = " "; // previous word; initialized to “not a word”
string current; // current word
while (cin>>current) { // read a stream of words
if (previous == current) // check if the word is the same as last
cout << "repeated word: " << current << '\n';
previous = current;
}
}
I don't understand how the program finds repeated word in a sentence, that you pass to console and gets read with 'cin', since 'cin' is supposed to take only first word and discard anything that comes after a whitespace. Can someone please explain me how the code (in particular the 'cin' part) works?
since 'cin' is supposed to take only first word and discard anything that comes after a whitespace.
That is wrong, wrong, wrong.
When you're using the formatted extraction operator on a string, the operation reads and extracts the first group of non-whitespace characters. Anything else in the stream is just.. not extracted. It is not discarded. It remains in the stream. Thus the next extraction operation grabs the next group of non-whitespace characters, ad nauseum.
You can input a sentence to the console to be read by 'cin', is what I meant.
Anything else in the stream is just.. not extracted. It is not discarded.
How does the while loop evaluate the cin>>current statement? How does it (after dealing with first word) know it should iterate further and deal with next words? And finally, when you press enter to input the sentence, why does it keep waiting for new input despite inputing new line?
Thank you
How does the while loop evaluate the cin>>current statement?
The expression cin >> current is evaluated. This results in a reference to cin. The language requires this to be converted to a bool in the context of a loop control expression (or an if condition.) Fortunately, this conversion is supplied by the class.
How does it (after dealing with first word) know it should iterate further and deal with next words?
By checking the state of cin via the mechanism described above. The expression will evaluate to true if the extraction succeeded and false if it did not, so the while loop continues until it is unable to extract more input. When dealing with cin and formatted string extraction, about the only thing that will cause it to fail is encountering an eof marker in the stream. On some systems (Windows) it must occur immediately after a newline.
when you press enter to input the sentence, why does it keep waiting for new input despite inputing new line?
Because encountering whitespace, which category your newline belongs to, doesn't cause the extraction to fail.
So there is a mechanism in the cin class that iterates word by word (or anything separated by whitespaces), and that allows while loop to run through all the "words" in the input sentence, and treat each of the words separately in each loop iteration?
Hehe ok, my language is appropriately noobish I know, but I can only rely on imagination when facing such a feature at this stage of learning. Thank you!