1. problem with strcmp

I have another question.

Let says I have 2 string
char a[10], b[10];

a and b have the same content.

I do
if(strcmp(a,b))
cout << "I love you";

It doesnt work.

It works when I use
if(strcmp(a,b)=='0')
cout << "I love you";

I am not allowed to use the == 0.

Anyone know the reason why?

2. If you want to compare for equality with strcmp, write:
Code:
```if (strcmp(a, b) == 0)
cout << "I love you";```
Notice that I wrote 0, not '0'. You could also write:
Code:
```if (!strcmp(a, b))
cout << "I love you";```
But I find it less intuitive because I find that the "==" in the former reminds me that this is a check for equality.

Originally Posted by byebyebyezzz
I am not allowed to use the == 0.
Huh?

3. k here is my real problem.
My professor provides us a test program for our assignment.
I am not allow to change anything in his test program. I can only look and try to figure out where the error is.

in his test program, he has something like

if(strcmp(a,b))
blah blah blah

I cant get the "blah blah blah" out put unless I use
if(strcmp(a,b)==0)

4. What is this assignment about? What do you understand by that line of your professor wrote?

Incidentally, is this supposed to be C or C++?

5. it's C++

just double check

if(strcmp(a,b)) is the same as if(strcmp(a,b)==0) ?

6. oops. I passed 1147/1147 test lol.

I think I got confused with all the
if(!strcmp(a,b))
if(strcmp(a,b))

It is much easier with the equal sign.

Can some1 explain them for me?

7. Originally Posted by byebyebyezzz
it's C++
Okay, just keep in mind that you're dealing with C-style null terminated strings.

Originally Posted by byebyebyezzz
if(strcmp(a,b)) is the same as if(strcmp(a,b)==0) ?
No.

Originally Posted by byebyebyezzz
I think I got confused with all the
if(!strcmp(a,b))
if(strcmp(a,b))
The idea is that when an integer x is converted to bool, it is as if you wrote x != 0 (i.e., zero is false, non-zero is true). Hence if (strcmp(a, b)) is like if (strcmp(a, b) != 0).

8. It's not the same. strcmp returns one of three things, and one of the possible three things (zero) means false: the other things mean true. A comparison, any comparison, like strcmp(a,b) == 0, changes the possible outcome. Now either strcmp has returned zero or not, and that determines what happens next.

In order to figure these things out you just have to know what the different Boolean operators do.

9. Thank for the info guys.
I am still learning