Basic string question

This is a discussion on Basic string question within the C++ Programming forums, part of the General Programming Boards category; I've got a basic string question that I'm sure the large majority of you will be able to answer, but ...

  1. #1
    Registered User
    Join Date
    Jun 2004
    Posts
    7

    Basic string question

    I've got a basic string question that I'm sure the large majority of you will be able to answer, but I haven't found something similar on these boards after an hour that would fix my problem.
    Code:
    string position;
    
    if (strcmp(position,"Home") == 0)
          {
          cout << "YES!!!!!";
          }
    ... gives me the error "cannot convert `std::string' to `const char*' for argument `1' to ".

    Any suggestions? Thanks.

  2. #2
    Registered User hk_mp5kpdw's Avatar
    Join Date
    Jan 2002
    Location
    Northern Virginia/Washington DC Metropolitan Area
    Posts
    3,799
    The string container has an overloaded == operator that would work. Alternately, you could convert the string to a const char* using the c_str member function:

    Either:
    Code:
    string position;
    
    if ( position == "Home" )
    {
        cout << "YES!!!!!";
    }
    Or:
    Code:
    string position;
    
    if (strcmp(position.c_str(),"Home") == 0)
    {
        cout << "YES!!!!!";
    }
    "Owners of dogs will have noticed that, if you provide them with food and water and shelter and affection, they will think you are god. Whereas owners of cats are compelled to realize that, if you provide them with food and water and shelter and affection, they draw the conclusion that they are gods."
    -Christopher Hitchens

  3. #3
    Registered User
    Join Date
    Jun 2004
    Posts
    7
    Sweet. It works perfectly. Thanks!!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. We Got _DEBUG Errors
    By Tonto in forum Windows Programming
    Replies: 5
    Last Post: 12-22-2006, 04:45 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. RicBot
    By John_ in forum C++ Programming
    Replies: 8
    Last Post: 06-13-2006, 06:52 PM
  4. String array question
    By gogo in forum C++ Programming
    Replies: 6
    Last Post: 12-08-2001, 05:44 PM

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