Problem with censor code variation of "Bleep" project, won't compile

I am on the “bleep” project and as with most projects, I will look at the solution and try to understand how it works and then do a variation off of it and make that work. I’ve been successful until this one. I can’t for the life of me figure out why this won’t work. I’ve done a lot of googling and am at my wits end with this one. If someone can show me why this isn’t working, I would be very grateful.

I am basically trying to create a vector with censored words. I tried a class first, but ran into the same issue I am now. I tried simplifying it down to just a function and same thing. Here is my code:

#include <iostream>
#include <string>
#include <vector>

void censor_check (std::string &text) {
using std::vector;
using std::string;
using std::cout;

  vector<string> censor_list {"broccoli", "Broccoli"};

  for (int c = 0; c < censor_list.size(); c++) {
      bool is_censored = false;

    for (int t = 0; t < text.length(); t++) {
      if (text[t] == censor_list[c]) {
        is_censored = true;

        if (is_censored) {
            for (int a; a < text.length(); a++) {
              text[t+a] = '*';
            }
         } 
        is_censored = false;
    } else {
          cout << "\n";
     }
  } 
}

int main() {

using std::cout;
using std::cin;
using std::vector;
using std::string;

      std::string input = "I like broccoli. Broccoli is green. If broccoli were a woman, I'd eat her.\n";

    censor_check (input);

    cout << input << "\n";

The line that seems to be the problem is:
if (text[t] == censor_list[c]) {

…but I don’t know why. Everywhere I look, this seems to be the right way to compare. Where am I going wrong?

Thanks in advance.

Hello!

When you iterate over std::string, you get single characters, not words. As a result, in the line if (text[t] == censor_list[c]) you are trying to compare a single character with a word. You need to split the input string into words before the loop.

Hello and thanks for your reply!

I’ve sorted out the issue I was having messing around with a few things, but now having trouble getting it to substitute asterisks where I want them and then getting the string back into the reference. I will try figuring this out a bit longer before asking for help again.

Silly me for picking such a hard language as my first one. :slight_smile: One thing I don’t like about C++ is that it seems many simple tasks are made to be so convoluted and confusing, needlessly even. This is no doubt due in part to me not knowing my way around as well as I’d like, but I feel like I have to learn how to think like a compiler or something.

Thanks again! Hopefully I can figure out what I’m doing wrong without needing any more assistance. :slight_smile:

I’m trying to use stringstream to break up the string into words and partially succeeding.

#include <iostream>
#include <string>
#include <vector>
#include <sstream>
#include <bits/stdc++.h>

// #include "functions.hpp"

void censor_check (std::string &text) {
using std::vector;
using std::string;
using std::cout;

  vector<string> censor_list {"broccoli", "Broccoli"};
  std::stringstream ss(text); // create stringstream object
  vector<string> temp_store;  // temp stores ss words
  string buf;     // temp storage for words extracted from ss
  string temp;   // temp storage for words to be censored

  int x = 0;
  bool is_censored = false;

      while (ss >> buf) {
          temp_store.push_back(buf);
      } // while loop end
      
      for (int t = 0; t < temp_store.size(); t++) {

        for (int c = 0; c < censor_list.size(); c++) {
          
          if (temp_store[t] == censor_list[c]) {
              is_censored = true;
              temp = temp_store[t];
          } // if end
            if (is_censored) {
                for (int a = 0; a < temp.length(); a++) {
                    temp[a] = '*';
               } // for [a] loop end
               temp_store[t] = temp;
            } // if i_c end
            is_censored = false;
      } // for [c] loop end
    } // for [t] loop end

      for (int z = 0; z < temp_store.size(); z++) {
        text = text + temp_store[z] + " ";
      }
  //    cout << text << "\n";
 } // censor_check function end


int main() {
using std::cout;
using std::string;

      string input = "I like broccoli. Broccoli is green. If broccoli were a woman, I'd eat her.\n";

    censor_check (input);
    cout << input << "\n";
/*
  censor_check ToCensor;
  ToCensor.check(text);

  for (int i = 0; i < text.length(); i++) {
    std::cout << text[i];
  }

  std::cout << "\n";  */

}

I got it to work better like this! However, it outputs this:

I like broccoli. Broccoli is green. If broccoli were a woman, I’d eat her.
I like broccoli. ******** is green. If ******** were a woman, I’d eat her.

I can’t figure out why it’s printing it twice; once as the original and once censored. If I put a cout in once, it prints twice. Why is it doing this? I’m trying to figure out how to have it censor the one with the delimiter on there without removing the delimiter from the string.

Edit:: Figured it out. I guess I was able to work myself through it after all. :slight_smile:

1 Like