C ++ is simple code. Find duplicate adjacent word

I need to write code that checks if the user enters the same word twice, and if so, he will display a message about what they did. So far I have had:

#include <iostream> using namespace std; int main(){ string previous = ""; string current = ""; while (cin>>current); { if(current == previous); { cout<<"repeated word"; } previous=current; } } 

It compiles, but it does not display a message when the user enters the same word twice.

+4
source share
1 answer

if you notice, your code contains ; in places that it should not contain. for example, if you put it after while (cin >> current) , then the code you want to run will not.

try the following:

 #include <iostream> using namespace std; int main(){ string previous = ""; string current = ""; while (cin>>current) { if(current == previous) { cout<<"repeated word"; } previous=current; } } 
+3
source

Source: https://habr.com/ru/post/1437080/


All Articles