C++Primer第五版 5.5.1节练习

练习5.20:编写一段程序,从标准输入中读取string对象的序列直到连续出现两个相同的单词或者所有单词都读完为止。使用while循环一次读取一个单词,当一个单词连续出现两次时使用break语句终止循环。输出连续重复出现的单词,或者输入一个消息说明没有任何单词是连续重复出现的。

#include<iostream>
#include<string>
using namespace std;
void main()
{
    string word, next;
    int count = 1;
    cout << "input strings " << endl;
    if(cin >> word)
    {
        while (cin >> next)
        {
            if (next == word)
            {
                cout << next << endl;
                count++;
                break;
            }
            else
            {
                word = next;
                count = 1;
            }
        }
        if (count == 1)
        {
            cout << " no " << endl;
        }
    }
}

猜你喜欢

转载自blog.csdn.net/alolf/article/details/81129901
今日推荐