String equality

This is a discussion on String equality within the C++ Programming forums, part of the General Programming Boards category; This should be easy. I'm admitting my newness by posting which for most will be, I hope, a simple question. ...

  1. #1
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    String equality

    This should be easy. I'm admitting my newness by posting which for most will be, I hope, a simple question.

    I'm having trouble testing a string for equality.

    Code:
    double myfunc(const char* z) {
        const char* mystr = "Test";
        double retVal = 100;
        if ( z == mystr) retVal = retVal * 2;
        return retVal;
    }
    This comes back with 100 and I'm looking for 200. Can somebody spot the error?

    Please don't make suggestions that involve changing the "const char* z" portion of line 1. I'm stuck with that.

    Thanks

    John

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    92
    They may be "C-style strings", but the variables themselves are just pointers. Therefore, if ( z == mystr) is asking if the address stored in z is equal to the address stored in mystr. Unless the caller of myfunc() happens to pass that value in somehow, your if() is going to be false.

    If you are stuck with const char* z then I would suggest reading up on strcmp().
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Success

    Then I shall read up on strcmp(). Thanks.

    John

    Thanks. It worked like a charm. Way cool.
    Last edited by JohnAnon; 03-25-2011 at 12:36 AM.

  4. #4
    Programming King Mr.777's Avatar
    Join Date
    Mar 2011
    Location
    Middle of NoWhere
    Posts
    320
    Use std::string or std::vector(preferable)
    Or compare them index by index.
    I don't care if someone doesn't like me, i was not put on earth to entertain everyone.

    No King, no Queen, I am the ACE of battle.

  5. #5
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Wouldn't have the foggiest

    As a noob to c++ I wouldn't have the foggiest notion of how to implement the "preferred" approach of using std::vector. Since I went to the bother of reducing my code to the smallest possible block that demonstrated the issue, if you could modify that block to show an example of how the std::vector approach would work that would be most appreciated.

  6. #6
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    I think Mr. 777 is incorrect. There is no need to use std::vector here, nor is it preferable, since you are comparing strings. std::string should suffice.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  7. #7
    Registered User
    Join Date
    Mar 2011
    Posts
    19
    Thanks Elysia. You're a programming master! How the hell do you know every thing?

    std:string worked just fine, too.

    Which is more efficient, std::string or using strcmp?

  8. #8
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    For comparison, I doubt you'll see a difference. Nevertheless, the general rule is that unless your program is running slowly and you can pinpoint the slowdown to a specific code that is attributed to std::string, there is no reason not to use std::string since it's the C++ alternative of C-style strings.
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  9. #9
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    What I'm passed may control

    I suppose the fact that I'm stuck with the pointer as an input means that I should go with the original suggestion (strcmp()) and only if I've got some other reason to do string manipulation, which would cause me to assign both of the values to c-style strings should I go with std::string.

  10. #10
    C++まいる!Cをこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,449
    You can wrap your C-style string inside a std::string object.
    std::string str(my_c_string);
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

  11. #11
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    I don't think both variables need to be std::string for == to work. This should work just as well:

    Code:
    double myfunc(const char* z) {
        const std::string mystr = "Test";
        double retVal = 100;
        if ( mystr == z) retVal = retVal * 2;
        return retVal;
    }
    "I am probably the laziest programmer on the planet, a fact with which anyone who has ever seen my code will agree." - esbo, 11/15/2008

    "the internet is a scary place to be thats why i dont use it much." - billet, 03/17/2010

  12. #12
    Registered User
    Join Date
    Mar 2011
    Posts
    19

    Smile

    Indeed it does. Would that it were that were the first suggestion.

    Many thanks. Learning, learning, learning.........

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 04:30 PM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 10:03 PM
  3. Classes inheretance problem...
    By NANO in forum C++ Programming
    Replies: 12
    Last Post: 12-09-2002, 02:23 PM
  4. creating class, and linking files
    By JCK in forum C++ Programming
    Replies: 12
    Last Post: 12-08-2002, 01:45 PM
  5. Warnings, warnings, warnings?
    By spentdome in forum C Programming
    Replies: 25
    Last Post: 05-27-2002, 06:49 PM

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