jeudi 11 juillet 2019

Noob question: Trying to censor specific words in a vector

I'm a noob and am on chapter 4 of this c++ book and I can't answer this assignment correctly.

The code that makes sense to me but doesn't work is included down below.

Here is the assignment:

Write a program that “bleeps” out words that you don’t like; that is, you read in words using cin and print them again on cout. If a word is among a few you have defined, you write out BLEEP instead of that word. Start with one “disliked word” such as string disliked = “Broccoli”; When that works, add a few more.

Help would be greatly appreciated, I might even mail you some cookies.

int main() {

vector<string> words;

for (string temp; cin >> temp; )

    words.push_back(temp);

string disliked = "broccoli";

for (int i = 0; i < words.size(); ++i)

    if (words[i] == disliked ||  disliked=="Bleep!")

        cout << words[i] << "\n";

Even if I type in several words all I get out ironically is "broccoli" and nothing else.

Aucun commentaire:

Enregistrer un commentaire