I want to know how a Console can respond in a Specific manner when the answer contains a specific character/words.
For example, If the Console asks: "How are you?" and the Response includes "Good" in the sentence, The Answer is different than when it includes "bad".
I tried using the Switch Function but I learnt it can only deal with integers. Then I went on to try the if Function:
1 2 3 4 5 6 7 8 9 10
string FeelingResponse;
cout << "\nHow are you feeling?" << endl;
cin >> FeelingResponse;
if (FeelingResponse == "Good")
{
cout << "I'm Glad!" << endl;
}
At the Moment, It only replies when the answer is 'Good'. My question is, how can it respond like this when the answer CONTAINS 'good'.
#include <iostream>
#include <string>
usingnamespace std;
int main()
{
string response;
cout << "How was your day?" << endl;
getline(cin, response); // To accept white spaces like "I had a good day"
// http://www.cplusplus.com/reference/string/string/find/
// http://www.cplusplus.com/reference/string/string/npos/if(response.find("good") != string::npos){ // If you rather not use string::npos, you can do (!= -1)
cout << "That's great to hear!" << endl;
}
else{
cout << "Hope you have a better day!" << endl;
}
return 0;
}