1

In the following piece of code, due to using a string, I'm unable to input 'word's into the vector 'words', as CTRL+Z (I'm using Windows) ends the program before I'm able to do so. I'm looking for an alternative to CTRL+Z or a way to change this program, that won't result in this problem. The program is meant to bleep out the words

int main()
{
    vector<string> bad_words = { };
    string word;
    cout << "Input words you want to bleep out ";
    for (string bad_word; cin >> bad_word;) {
        bad_words.push_back(bad_word);
    }
    vector<string> words;
    cout << "Input words you want to check ";
    for (; cin >> word;) {
        words.push_back(word);
    }
    for (int i = 0; i < words.size(); ++i) { // for each word in words
        bool contains = false; //  contains = false
        for (string bad_word : bad_words) { //   for each bad_word in bad_words
            if (word == bad_word)
                contains = true;
            break;
        }
        if (contains == true)
            cout << "Bleep ";
        else
            cout << word;
    }
}

This is the Exercise:

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.

The 'break' in the inner for-loop is for the purpose of exiting the loop. I've added it because I believed that the loop would run infinitely otherwise (word == bad_word no matter how many times you run it)

Alumi
  • 11
  • 2
  • Use a teminator string of your choice, like "END" or "0" or "Z" or "." whatever and compare for that string in the input loop. – L. Scott Johnson Feb 14 '21 at 18:55
  • That's a rather unusual way of structuring a for loop that I've never seen before, relying on a side effect of the end condition. – Mark Ransom Feb 14 '21 at 19:14
  • See [Resetting the State of a Stream](https://stackoverflow.com/q/11246960/5987). – Mark Ransom Feb 14 '21 at 19:18
  • Do you intentionally cancel the inner for loop after the first iteration? – fabian Feb 14 '21 at 21:28
  • @L.ScottJohnson I don't know what do you mean by this. Could you please clarify? – Alumi Feb 15 '21 at 19:28
  • @MarkRansom What do you mean by the side effect ot the end condition? Also, I fail to see how does the answer that you have provided answer my question. This may be because I'm on a very beginner level and don't even understand the question from your hyperlink (I don't know what fail bit is). Would you please attempt to simplify it for me? – Alumi Feb 15 '21 at 19:28
  • The middle part of a `for` is generally an expression that returns true/false for whether to stop the loop; you're also using it to set another variable (the side effect). The link I gave you is for how to keep reading from stdin after you've received a Ctrl-Z. – Mark Ransom Feb 15 '21 at 19:35
  • @asdfghjkl I mean: you need some way of indicating the end of the first input. You can't end the stream since you want to use it later to read in the test text. So instead specify a "magic" word that you'll use to mark the end of the bad words. When the user inputs that, you end the first loop. – L. Scott Johnson Feb 15 '21 at 20:03
  • @MarkRansom What's the alternative? How else do you input values into a vector? – Alumi Feb 16 '21 at 17:34
  • Most people would use a `while` loop instead of a `for`: `while (cin >> bad_word) bad_words.push_back(bad_word);`. Nothing wrong with your way, it works I'm sure, just unusual as I said. – Mark Ransom Feb 16 '21 at 18:00

0 Answers0