这次参考之前C++primer的习题答案,的确比较方便:
问题是比较输入一系列string,如果发现连续相同的string则输入结束,否则循环进行:
#include <iostream>
#include<string>
using namespace std;
int main()
{
string cur,pre;
bool flag = true;
cout << "Please input string: " ;
while (cin >> cur )
{
if (cur == pre)
{
if(!cur.empty())
cout << "The repeat words are "<<cur <<endl;
else
cout << "There is no repeated word" << endl;
break;
}
else
{
cout << "Please input string: " ;
pre = cur;
}
}
}