strcmp() bool

Hello,

In my program, I am comparing a cstring with strcmp().

if(!strcmp(answer,"c") || !strcmp(answer,"create")){
//...
}

If the user enters "c", the program works; if the user enters "create" the program does not work.

Thanks for your help.

Edit: Fixed bracket.
Last edited on
strcmp doesn't return a bool. It returns an int that is 0 if the strings are equal.
Looks to me like this (which in your sample above is missing a bracket)

(!strcmp(answer,"c") || !strcmp(answer,"create"))

will ALWAYS come out as true. Can you think of a value that answer can have that will simultaneously be c and create ? Because that's the only way this won't come out as true.
Last edited on
if first statement in if( first || second ) is true then other won't be checked

'c' is character
"c" is not, it's const char*
OK...

So how would I check both arguments correctly?
It looks like it should work, but is better written as:
1
2
3
4
if( strcmp(answer, "c") == 0 || strcmp(answer, "create") == 0 ) {
    //then answer was either equal to "c" or "create"
    //...
}

I'm not sure why it's not; check spelling? Maybe explain how it's not working?
Topic archived. No new replies allowed.