Jan 23, 2012 at 1:08am Jan 23, 2012 at 1:08am UTC
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 Jan 23, 2012 at 1:39am Jan 23, 2012 at 1:39am UTC
Jan 23, 2012 at 1:11am Jan 23, 2012 at 1:11am UTC
strcmp doesn't return a bool. It returns an int that is 0 if the strings are equal.
Jan 23, 2012 at 1:11am Jan 23, 2012 at 1:11am UTC
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 Jan 23, 2012 at 1:12am Jan 23, 2012 at 1:12am UTC
Jan 23, 2012 at 1:14am Jan 23, 2012 at 1:14am UTC
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 *
Jan 23, 2012 at 1:42am Jan 23, 2012 at 1:42am UTC
OK...
So how would I check both arguments correctly?