初学者:While 循环无法正常工作

beginner :While loop not working as it should

我还是个初学者,正在看书学习。有一个练习要求我根据过滤词向量过滤输入,如果它是其中一个,它会输出 "bad word"

这是和书上一样的练习。

Try This
Write a program that “bleeps” out words that you don’t like; that is, you read in words using cin and print them again on cout. If a word is among a few you have defined, you write out BLEEP instead of that word. Start with one “disliked word” such as string disliked = “Broccoli” When that works, add a few more.;

这是我写的代码:

#include <D:\std_lib_facilities.h>

int main()
{

    // RL: omitting actual "bad" words to protect the innocent...
    vector <string> bwords { "word1", "word2", "word3" };

    vector <string> words;
    string input = "";

    while(cin >> input)
    {
        words.push_back(input);
    }

    double counter1 = 0;
    double counter2 = 0;

    while(counter1 < bwords.size() && counter2 < words.size())
    {
        if(bwords[counter1] == words[counter2])
        {
            cout << " bad word ";
        }
        else if (counter1 == bwords.size() - 1 && counter2 != words.size() )
        {
            cout << " "<< words[counter2] <<" ";
            counter1 = 0;
        }
        else
        {
            ++counter1;
            counter2 += 1 / bwords.size();
        }
    }
}

无论何时启动,它都只测试第一个单词并重复它自己,就像只测试第一个 if 条件一样。

您使循环过于复杂。试试这样的东西:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

int main()
{    
    vector <string> words;
    string input;

    while (cin >> input)
        words.push_back(input);

    for (int counter = 0; counter < words.size(); ++counter)
        cout << " " << bleepWordIfBad(words[counter]) << " ";

    /*
    Alternatively:

    for (vector<string>::iterator iter = words.begin(); iter != words.end(); ++iter)
        cout << " " << bleepWordIfBad(*iter) << " ";
    */

    /*
    Alternatively:

    for (const string &word : words)
        cout << " " << bleepWordIfBad(word) << " ";
    */

    return 0;
}

或者,完全摆脱手动循环:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

void outputWord(const string &word)
{
    cout << " " << bleepWordIfBad(word) << " ";
}

int main()
{    
    vector <string> words;
    string input;

    while (cin >> input)
        words.push_back(input);

    for_each(words.begin(), words.end(), outputWord);

    /*
    Alternatively:

    for_each(words.begin(), words.end(),
        [](const string &word) { cout << " " << bleepWordIfBad(word) << " "; }
    );
    */

    return 0;
}

或者,完全删除输入 vector 并在输入时过滤用户的输入:

#include <vector>
#include <string>
#include <algorithm>

using namespace std;

// RL: omitting actual "bad" words to protect the innocent...
const vector <string> bwords { "word1", "word2", "word3" };

string bleepWordIfBad(const string &word)
{
    if (std::find(bwords.begin(), bwords.end(), word) != bwords.end())
        return "BLEEP";
    else
        return word;
}

int main()
{    
    string word;

    while (cin >> word)
        cout << " " << bleepWordIfBad(word) << " ";

    return 0;
}