Beginner here.
I am writing a program, and I want it to take my input and see if what I typed contains a certain keyword, like "hello", for example.
I know how to make it do something if my input is exactly what it's looking for, like if I typed "hello" exactly, but what if I typed "hello there"?
It's not exactly "hello", but my input contains it -- so, how do I make the program recognize if that word is in the input at all?
I am trying to make a simple "chatbot" (obviously not real AI, just an idea). This is what I have so far:
string ans;
cout << "Hello, I am Oliver, a Chatbot" <<endl;
cout << "What can I do for you today?" <<endl;
cin >> ans;
How do I make it check if the cin, ans, contains a specific word, like "hello"?
CodePudding user response:
Use std::string::find(), eg:
string ans;
cout << "Hello, I am Oliver, a Chatbot" <<endl;
cout << "What can I do for you today?" <<endl;
cin >> ans;
if (ans.find("hello") != string::npos) {
// found
}
else {
// not found
}
