problem with strcmp

This is a discussion on problem with strcmp within the C++ Programming forums, part of the General Programming Boards category; I have another question. Let says I have 2 string char a[10], b[10]; a and b have the same content. ...

  1. #1
    Registered User
    Join Date
    Jun 2011
    Posts
    51

    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. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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.

    Quote Originally Posted by byebyebyezzz
    I am not allowed to use the == 0.
    Huh?
    Last edited by laserlight; 06-27-2011 at 08:53 PM.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    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. #4
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    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++?
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  5. #5
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    it's C++

    just double check

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

  6. #6
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    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. #7
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,712
    Quote Originally Posted by byebyebyezzz
    it's C++
    Okay, just keep in mind that you're dealing with C-style null terminated strings.

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

    Quote 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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,689
    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. #9
    Registered User
    Join Date
    Jun 2011
    Posts
    51
    Thank for the info guys.
    I am still learning

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp problem in c
    By Prodiga1 in forum C Programming
    Replies: 2
    Last Post: 11-22-2010, 11:13 PM
  2. strcmp problem, whats the problem, i cant figure it out!
    By AvaGodess in forum C Programming
    Replies: 14
    Last Post: 10-18-2008, 06:45 PM
  3. strcmp problem
    By cstudent in forum C Programming
    Replies: 3
    Last Post: 04-15-2008, 09:48 AM
  4. Problem with strcmp()....
    By LightsOut06 in forum C Programming
    Replies: 3
    Last Post: 09-02-2005, 12:30 PM
  5. problem with strcmp
    By maditsi in forum C Programming
    Replies: 1
    Last Post: 12-06-2001, 11:38 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21