I assume you'd like to have a random response for, lets say, when the user asks where you're from. possibleanswers could be like "My creator", "My mom", or "I just happened".
you could define another set of answers at the top like so:
std::string randWhereFrom[3] = {"My Creator", "My Mom", "I just happened"};
and then you could add it after the last if statement like so:
1 2 3
|
getline (std::cin, mystr);
if (mystr == "Where are you from?")
std::cout << randWhereFrom[rand() % 3];
|
To learn more about what's going on here is that when you call the function
rand()
it returns a random number based on the seed given by
srand()
. This number can be a huge number, but you only care about numbers 1-x, in my example, 1-3. The modulus, %, returns the remainder when divided by x, in my example, a random number is divided by 3 and you will get either a remainder of 0, 1, or 2. That number corresponds to the position in the array of randWhereFrom.
Understand?