int main()
{
string previous = " ";
string current;
while (cin>>current)
{
if(previous==current)
cout << "repeated word: " << current << ".\n";
previous=current;
}
return 0;
}
the point of this code (found in my textbook) is to find repeated words in a inputted string.
in the while, you have the if statement, it isn't true, so it skips it then it runs this
previous=current;
setting previous equal to the current variable.
then the while repeats, which I understand, but at this point it goes strait to the if statement without asking for another input. why does it do that? and how does it find the repeated word? if you set previous equal to current, wouldn't the if statement always be true, displaying the entire string and not just the repeated word?