c++debuggingvectoruser-inputfindandmodify

Compare user input with values stored in vector


This is my program thus far. It compiles but gets stuck and crashes on the last part. I want to repeat user's string input and replace any bad words found in the string with "****". Most likely my error is in find_Poop_inSentence. "Debug Assertion failed. vector subscript out of range"

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

  }
}

Solution

  • Well thanks to my friend Ivan Drago I was able to solve this.

    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";
    cout << "Push enter twice when done.\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) {
    
    for (unsigned int i = 0; i < v1.size(); i++) {
    
        for (unsigned int j = 0; j < v2.size(); j++) {
            if (v1[i] == v2[j]) {
                v2[j] = sub;
            }
    
        }
      }
    
    }