Keyword Responses
- For this component, add a new class called keyword. It is very
similar to random. The main difference is that here, you need
to have two arrays, one for the responses, and one for keywords. For
example, suppose that the first keyword is "love". If it appears in
the user's input, you will output a pre-stored phrase that is somewhat
related to the keyword. One such phrase might be: "Everybody loves
somebody sometime."
- This class also has a member function called reply In
reply, you want to check whether your keywords appear in the
user's response. You want to stop once you determined that a keyword
appears in the user's response. In that case, you print the associated
response to the screen and your function should return "true". Recall
a string member function that we have seen in class. Suppose you have
a string variable foo.
- foo.find("hello", 0) starts at the first position in the
string and looks for the string "hello" in the string assigned to
foo. It returns the starting position of "hello" in
foo, if "hello" can be found. Otherwise, it returns npos,
a constant that is defined in the class string. Usually npos is
-1, but you cannot reply on that. So instead, simply refer to it in
the following way: string::npos
- Test your keyword component by declaring a keyword object in
your main function and call the function reply with a string that you
make up. Call it with a string that contains a keyword from your
database, as well as with a string that does not contain a keyword.