If statement error with string [SOLVED]

I’m having a problem with my if statements. What I’m trying to do is, if string 1 equals the string 2[0], then string 3=string 4. Or if string 1=string2[1], then string 3= string 5.
But for some reason it keeps running an error with the strings in the if statement. I don’t understand what’s wrong with my code, it seems to be written correctly. I tried both equaling string 1 to the actual content of string 2 but that doesn’t work either.

void ofApp::draw(){

int i;
ofSeedRandom();
srand(time(NULL));
int b=ofRandom(0, 4);
int c=ofRandom(0, 5);
string category[4]={"Celebs", "Animals", "Countries", "Movies"};
string cat=category[b];
string Celebs[5]={"eminem", "pink", "madonna", "cher", "prince"};
string Animals[5]={"elephant", "kangaroo", "cheetah", "zebra", "giraffe"};
string Countries[5]={"indonesia", "austria", "brazil", "scotland", "thailand"};
string Movies[5]={"terminator", "lincoln", "taken", "gladiator", "divergent"};
string word;

if (cat="Celebs")
{
    word=Celebs[c];
 }

 if (cat=category[1])
{
    word=Animals[c];
}
if (cat=category[2])
{
    word=Countries[c];
}
if (cat=category[3])
{
    word=Movies[c];
}

This is the error that comes up:

error: could not convert ‘cat.std::basic_string<_CharT, _Traits, _Alloc>::operator=<char, std::char_traits, std::allocator >(((const char*)“Celebs”))’ from ‘std::basic_string’ to ‘bool’

For all the if statements.

Note:
If i take out the if statement and just run cat=“celebs” or word=Celebs[c], it runs just fine. Also, for some reason when I print the word=Celebs[c], when it prints to the screen it scrolls through all the strings, even thought I have srand(time(NULL)). So when it prints, it prints “Eminem”, then “pink” , etc. instead of just sticking to one. Anyone know how to fix this?

When using if statements to compare values you need to use the logical operator == not the assignment = operator.

Oh man, can’t believe I missed that. Thanks!

1 Like