【问题标题】:beginner :While loop not working as it should初学者:虽然循环不能正常工作
【发布时间】:2015-08-21 01:36:34
【问题描述】:

我还是个初学者,我正在从一本书中学习。有一个练习要求我根据过滤词向量过滤输入,如果它是其中一个,它会输出“坏词”

这是与书中完全相同的练习。

试试这个
编写一个程序来“发出”你不喜欢的单词;也就是说,您使用 cin 读取单词并在 cout 上再次打印它们。如果一个词在你定义的几个词中,你写出 BLEEP 而不是那个词。从一个“不喜欢的词”开始,例如字符串 disliked = “Broccoli” 当它起作用时,再添加一些。;

这是我写的代码:

#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 条件一样。

【问题讨论】:

  • 您是否在调试器中单步执行代码,并举例说明流程和变量值,或者打印出计数器的值、if 语句中的位置等?学习如何调试与学习语言一样重要。
  • counter1 和 counter2 仅在您陷入 else 时才会递增。如果你检查的第一个工作是坏的,那么它永远不会增加并寻找下一个
  • 我还没有详细查看代码,但让我印象深刻的第一件事是为什么将所有输入单词读入一个向量然后再遍历它?为什么不读取一个单词,检查它,产生适当的输出,然后循环读取下一个单词?并将检查单词是否错误的代码放入函数中。然后你最终得到两个死的简单循环(最多) - 一个在函数中检查一个单词是否是坏的(注意你可能不需要一个循环,这取决于你是否被允许使用一个容器以外的容器vector) 和为每个输入单词调用函数的主循环。

标签: c++ loops vector while-loop


【解决方案1】:

您的循环过于复杂。尝试更多类似的方法:

#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;
}

【讨论】:

  • 感谢您指出向量的其他子函数,因为本书中唯一写的就是 size() 子函数,至少目前是这样
  • 哇,有很多变化!
  • @StilesCrisis:欢迎使用 C++,有几十种不同的方法可以完成同一件事! :)
  • 大声笑,我已经专业编写 C++ 15 年了。我更想说的是你在帖子上花费的精力:)
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2022-01-12
  • 2012-08-12
  • 1970-01-01
  • 2021-05-12
  • 2012-11-27
相关资源
最近更新 更多