I'm a beginner in C++ and I've using Programming: Principles and Practice from Bjarne Stroustrup. I've gotten to the assignment chapter and came across this example
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;
}
}
But I don't quite understand the logic behind it, mainly in the first through of the loop, where previous=" ", i dont know how previous get to equal anything at all from the input in current given that it values " ". If I compile and input " the cat cat eats" it identifies cat as the repeated word but how does it know since, like the comment says, its initialized to no word, why does it cout the repeated word: cat in that first case.