Thread: Comparing string contents

  1. #1
    Registered User
    Join Date
    Jan 2004
    Posts
    11

    Comparing string contents

    What I want is to be able to take the user input with cin and store it in a string. Then, by using a function, check to see if the string matches something.

    To clarify

    Code:
    string input;
    cin >> input;
    Then I want to check to see if "input" matches another string, to give a conditional response. How would I set up that part? I tried creating other strings and placing if stements that compared them, but I think it said that it couldn't convert them into boolean statements.



    Sorry if I'm being too confusing. Thanks for any help you can give me.

  2. #2
    Registered User
    Join Date
    Nov 2001
    Posts
    1,348
    Post all the code including the comparison code.

    Kuphryn

  3. #3
    Registered User
    Join Date
    Sep 2003
    Posts
    135
    std::string comparison uses ==, e.g.

    Code:
    std::string str1;
    std::cin >> str1;
    
    std::string str2;
    std::cin >> str2;
    
    if(str1 == str2) {
      std::cout << "Strings match" << std::endl;
    } else { 
      std::cout << "Strings don't match" << std::endl;
    }

  4. #4
    Registered User
    Join Date
    Jan 2004
    Posts
    11
    Ok, the == was what I was missing. Thank you.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Inserting contents of string into an array
    By Gaming in forum C++ Programming
    Replies: 4
    Last Post: 04-13-2008, 10:31 AM
  2. Message class ** Need help befor 12am tonight**
    By TransformedBG in forum C++ Programming
    Replies: 1
    Last Post: 11-29-2006, 11:03 PM
  3. Calculator + LinkedList
    By maro009 in forum C++ Programming
    Replies: 20
    Last Post: 05-17-2005, 12:56 PM
  4. problems with overloaded '+' again
    By Brain Cell in forum C++ Programming
    Replies: 9
    Last Post: 04-14-2005, 05:13 PM
  5. "Operator must be a member function..." (Error)
    By Magos in forum C++ Programming
    Replies: 16
    Last Post: 10-28-2002, 02:54 PM