william_
william_

Reputation: 1133

is this not using boolean operators in c++?

I have some code which is incorrect because i have not used boolean operators However i thought the boolean operators in c++ were && and ||

here is the question:

Enhance your search algorithm so that a query may contain Boolean operators – AND and OR. For instance, a query “word1 AND word2” means to find all the ideas, each of which contains both of the words. A query “word1 OR word2” means to find all the ideas, each of which contains either word1 or word2.

and here is my code just for the and operator

void IdeaBank::AND_searchQuery(string search_word1, string search_word2){

vector <int> match_AND;
for (int i=0;i<newIdea.size();i++)
{ 
    if (newIdea[i].foundWordInBoth(search_word1) && newIdea[i].foundWordInBoth(search_word2))
        {
            match_AND.push_back(newIdea[i].getID());
        }   
}

}

i just changed the && symbol to || to check for the "or" part

is there something i am missing or am i not using the boolean operators correctly?

this is for my school project so my teacher was unable to give me any answers.

EDIT: the function foundWordInBoth is a boolean function and there is no error with the code.

i showed my teacher my code and he said no that way is too easy you need to use boolean operators, which is why im confused

i looked online and there were a few source codes that use boolean algebra where they assign boolean variables numbers. could this be what he meant?

Edit: this was the response i got after sending an email

boolean expression not boolean operators. A boolean expression (formula) is a combination of boolean operators (AND, OR and NOT). Any formula in while statement or if statement is a boolean expression. Once you have functions for AND and OR, simply parse the expression and then combine the outcomes. If have functions for AND and OR... is parse the expression to combine them into one function?

Can someone help explain?

Upvotes: 4

Views: 276

Answers (2)

Roy2511
Roy2511

Reputation: 1038

EDIT: the function foundWordInBoth is a boolean function and there is no error with the code.

i showed my teacher my code and he said no that way is too easy you need to use boolean operators, which is why im confused

Your code seems fine to me. Probably your teacher wants you to use the bitwise boolean operators & and | instead of the logical operators && and ||.

In that case true & true = 1, true | false = 1 ...

Upvotes: 1

baohiep
baohiep

Reputation: 51

If foundWordInBoth checks for the existence of a word and returns a boolean, then your code is correct.

Upvotes: 1

Related Questions