Thread: problem with strcmp

  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
    28,413
    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.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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++?
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    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
    28,413
    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).
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  8. #8
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    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-23-2010, 12:13 AM
  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-07-2001, 12:38 AM