Trouble with vectors

Hello,

I'm teaching myself C++ and working through one of Stroustrup's books. I'm currently on the section of vectors, and am trying to write a simple program that prints words I enter into the vector, BUT will print "Bleep" on words of my choosing. Using single words I can get the code to work, but I'm pretty sure the purpose of the exercise is to build a 2nd "bad words" vector that is getting compared to the first vendor to find and "bleep" bad words. Below is the code I have so far that is working (sort of). I've tried a few different "for" statements for the 2nd vector and have gotten semi-close but I can't seem to close the gap. Any hints or insights you can provide here would be greatly appreciated. I'm not finding good enough examples in the book to work backwards from. Thanks!

1
2
3
4
5
6
7
8
9
10
11
12
13
 int main()
{
	vector<string>words;
	for (string temp; cin >> temp;)
		words.push_back(temp);

	vector<string>bad_words;
	bad_words.push_back("apple");
	bad_words.push_back("pear");

	for (int i = 0; i <= words.size(); ++i)
		if (words[i] == "apple") cout << "Bleep\n"; else
		cout << words[i] << '\n';
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
#include <iostream>
#include <vector>
#include <string>

int main()
{
    const std::vector<std::string> bad_words = { "apple", "pear", /* ... */  } ;

    std::vector<std::string> words ;
    for( std::string temp ; std::cin >> temp ; ) words.push_back(temp) ;

    for( const std::string& word : words ) // for each word in the vector
    {
        bool is_bad = false ;
        // check if this word is in the list of bad words
        for( const std::string& bad : bad_words ) // for each bad word
            if( word == bad ) { is_bad = true ; break ; }

        if( is_bad ) std::cout << "Bleep\n";
        else std::cout << word << '\n' ;
    }

    // or using classic for loops
    for( unsigned int i = 0 ; i < words.size() ; ++i ) // for each word in the vector
    {
        bool is_bad = false ;
        // check if this word is in the list of bad words
        for( unsigned int j = 0 ; j < bad_words.size() ; ++j )  // for each bad word
            if( words[i] == bad_words[j] ) { is_bad = true ; break ; }

        if( is_bad ) std::cout << "Bleep\n";
        else std::cout << words[i] << '\n' ;
    }
}
First in your for-loop , your condition should be i < words.size().

Now , your question ,
You have to compare your word against each bad word , i.e. search for words[i] in bad_words.
This should go within your for-loop :
If you are introduced to algorithms , then ...
1
2
3
4
5
        auto res = std::find(bad_words.begin(),bad_words.end(),words[i]);
        if(res != bad_words.end())
            cout << "Bleep !" << endl;
        else
            cout << words[i] << endl;

otherwise you could do something like this.
1
2
3
4
5
6
7
8
9
10
11
int j = 0;
for(; j < bad_words.size() ; ++j)
{
    if(words[i] == bad_words[i])
    {
        cout << "Bleep !" << endl;
        break;
    }
}
if(j == bad_words.size())
    cout << words[i] << endl;


Hope that helps.
Last edited on
Thanks I'll give these a try!
Topic archived. No new replies allowed.