Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Mar 2008
    Posts
    2

    Question Help with strings

    Ok, I'm making a chatter bot (I know, tall order, but I think I can make a simple one) and this is what I have so far:

    Code:
    #include <iostream>
    
    using namespace std;
    
    int main(int argc, char *argv[])
    {
      char One[50], Two[50], Three[50];
      cout<<"//-- Hello, my name is Deep Blues.\n>> ";
      cin.getline ( One, 50 );
      if(One == "My name" || One == "my name") {
             cout<<"//-- Nice name.\n>>";
             cin.getline ( Two, 50 );
             cout<<"//-- Ok then "<< One <<", What do you want to talk about?\n>> ";
             cin.getline ( Three, 50);
             } else {
             cout<<"//-- What is your name?\n>> ";
             cin.getline ( Two, 50 );
             cout<<"//-- Ok then "<< Two <<", What do you want to talk about?\n>> ";
             cin.getline ( Three, 50);
             }
    }
    How can I make it so the text in the if statement looks for the text "my name" in the string. Like the find function in notepad but only for the specific string?

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Firstly, you can make your life easier by using std::string instead of null terminated C-style strings.

    std::string has a find() member function that you can use, but it is case sensitive. What you can do is to make both the input string and the search string lower case (or upper case, if you prefer), then use find().
    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
    Mar 2008
    Posts
    2
    erm, can you elaborate,
    i.e change my code for me so I can better understand what you're talking about.

  4. #4
    Kiss the monkey. CodeMonkey's Avatar
    Join Date
    Sep 2001
    Posts
    937
    http://en.wikipedia.org/wiki/Std::string

    Just think about it. The following code will be true only if the pointer known as One points to the exact same location in memory as a string literal ("My name" or "my name") that your program loaded into memory.
    Code:
    One == "My name" || One == "my name"
    For what you want, you would need to compare each character in memory after what the char* points to. std::strcmp() does this, but you'd be MUCH better off using the class type std::string, with which
    Code:
    One == "My name" || One == "my name"
    would mean exactly what you expect.

    But what you're looking for would involve
    http://www.cppreference.com/cppstring/find.html
    Last edited by CodeMonkey; 03-15-2008 at 11:31 PM. Reason: adition
    "If you tell the truth, you don't have to remember anything"
    -Mark Twain

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM