-
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?
-
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().
-
erm, can you elaborate,
i.e change my code for me so I can better understand what you're talking about.
-
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