在过去的一个小时里,我一直在努力:
#include <iostream>
#include <vector>
#include <string>
/* This program filters disliked words (by the user), and replace it with a "BLEEP!". */
int main() {
std::cout << "Enter a list of words: ";
std::vector<std::string> list_of_words; //use vector to store the list of words
for (std::string temp_i; std::cin >> temp_i;) {
list_of_words.push_back(temp_i);
}
std::cout << "Number of words: " << list_of_words.size() << '\n' << std::endl;
std::string temporary_input2 = " ";
std::cout << "Enter a list of disliked words: ";
std::vector<std::string> disliked_words; //stores the list of disliked words
for (std::string temp_i2; std::cin >> temp_i2;) {
disliked_words.push_back(temp_i2);
}
std::cout << std::endl;
std::string temp_output = " ";
for (int i_counter = 0; i_counter < list_of_words.size(); ++i_counter) {
std::string word_list_temp = list_of_words[i_counter];
//cycle through disliked_words and compare each of them to the current word_list_temp
for (std::string disliked_word_temp : disliked_words) {
if (word_list_temp == disliked_word_temp) {
temp_output = "BLEEP!";
break; down below
} else if (word_list_temp != disliked_word_temp) {
temp_output = word_list_temp;
}
std::cout << temp_output << std::endl; //will vary
}
}
std::cout << "\nReached end of program." << std::endl;
}
它可以完美地编译所有内容(幸运的是,编译器没有抱怨)。但是,问题只是在第一阶段就开始了。当程序要求用户“输入单词列表”时。我输入了4个字,然后按CTRL +D。这是输出:
输入单词列表:嗨,再见,你好
字数:4
输入不喜欢的单词的列表:
到达程序的结尾。
此外,代码可能有点混乱(效率低下。我也想知道如何改进代码!)。您的回答将不胜感激。谢谢。
(编辑:我希望程序可以过滤掉list_of_words中声明的所有单词,但是,程序在输出的第一行要求输入后立即跳到循环的末尾,这确实是意外)