這是我迄今的程序。它編譯但在最後部分卡住並崩潰。我想重複用戶的字符串輸入並用「****」替換在字符串中找到的任何不好的單詞。很可能我的錯誤在find_Poop_inSentence中。 「調試斷言失敗,向量下標超出範圍」將用戶輸入與存儲在向量中的值進行比較
void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub);
int main()
{
cout << "Howdy partner, tell me some words you don't take kindly to.\n";
vector<string>bad_words;
string word;
while (cin >> word)
{
cin.ignore();
bad_words.push_back(word);
if (word == "exit")
break;
}
cout << "Ok partner, got it!\n";
cout << "Now say something and I'll repeat it back to you. Don't worry, I'll bleep out the words that you don't like.\n";
word = "";
vector<string> random_sentence;
while (cin >> word)
{
cin.ignore();
random_sentence.push_back(word);
if (cin.get() == '\n')
break;
}
find_Poop_inSentence(bad_words, random_sentence, "****");
cout << "You said: ";
for (unsigned int i = 0; i < random_sentence.size(); ++i) {
cout << ' ' << random_sentence[i];
}
system("Pause");
return 0;
}
void find_Poop_inSentence(vector<string> & v1, vector<string> & v2, string sub) {
int iterOne;
int iterTwo = 0;
int iteratorMax = v2.size();
for (iterOne = 0; iterOne < iteratorMax; iterTwo++) {
if (v1[iterOne] == v2[iterTwo]) {
v2[iterTwo] == sub;
}
if (iterTwo == iteratorMax) {
iterOne++;
iterTwo = 0;
}
}
}
fml。將編輯並重新發布。抱歉。一直在努力,沒有太多的睡眠,我的大腦受到傷害。迴應 – Ranfan
需要一段時間來編輯,但我仍然處於困境 – Ranfan
你有沒有跟你的橡皮鴨說話,但是,正如我建議的那樣? –