Another help needed

Why does the word "bot" and the "censored" word print when it is not supposed to?

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
35
36
  #include "std_lib_facilities.h"
using namespace std;

int main()
{
 vector <string> word;      //Vector for input
 vector <string> bword(6);  //Vector for censoring
 string in;

 bword[0]="shark";
 bword[1]="zone";
 bword[2]="broccoli";
 bword[3]="fox";
 bword[4]="nope";
 bword[5]="bot";     // The bunch of words that should be censored

cout<<"Ready. (Cntrl+z to terminate loop for Windows)(Cntrl+D for Unix)"<<endl;

 while (cin>>in)
    word.push_back(in); // Input words

 cout<<"Number of words: "<<word.size()<<endl;

 sort(word.begin(),word.end());  //Ignore: this only arranges inputs in 
                                //alphabetical order
 for (int c=0;c<word.size();++c)    
    {
    if (c==0||word[c-1]!=word[c]&&word[c]!=bword[0]&&word[c]!=bword[1]&&word[c]!=bword[2]&&word[c]!=bword[3]&&word[c]!=bword[4]&&word[c]!=bword[5])  // Conditions for printing word 
       cout<<word[c]<<endl;

    if (word[c]==bword[0]||word[c]==bword[1]||word[c]==bword[2]||word[c]==bword[3]||word[c]==bword[4]||word[c]==bword[5])              // Conditions for "not" printing
        cout<<"WHATCH YOUR TOUNGE/KEBOARD"<<endl;
    }
    return 0;
}
line 28...

if c==0 or etc, well c is = to 0 at first so print word.

This is my guess.
It doesn't print shark, but it does print bot.

Try to erase some vectors and their conditions, you will get different outputs.
closed account (o3hC5Di1)
Hi there,

I suggest you use std::find ( http://www.cplusplus.com/reference/algorithm/find/ ) to determine whether a word needs to be censored. It will make your intent much clearer and it will most likely solve your problem.

Example for your code:

1
2
3
4
5
6
7
8
if (std::find(bword.begin(), bword.end(), word[c]) != bword.end())
{
    //word needs to be censored
}
else
{
    //word does not need to be censored
}


All the best,
NwN
Last edited on
Thanks, but I don't know how to use std:: yet. Any other solutions if you don't mind?
closed account (o3hC5Di1)
If you are using sort(word.begin(),word.end()); you might as well use std::find. I pretty much gave you the code, if you think it over I'm sure you can figure this out. Hint: it goes in your for-loop.

If you need any further help please come back to us with your attempt of implementing this.

All the best,
NwN
I think the important point is the use of if - else rather than the original code which had two independent if statements,
what part of std:: that you don't understand?
since at the start of your code you say using namespace std;

now you don't need to put std::
Topic archived. No new replies allowed.