Question: Convert the magic eight ball program from the arrays module to use a vector instead of an array. Be sure to do these things in
Convert the magic eight ball program from the arrays module to use a vector instead of an array. Be sure to do these things in your program:
use the push_back() function to initialize the array
modify the signature and prototype of the getAnswer() function
modify the code in the body of the getAnswer() function
remove any unneeded code, such as your constant for the number of answers
Here is the magic 8 ball code here:
#include
#include
#include
#include
#include
#include
#include
using namespace std;
string getAnswer();
const string exitString = "x";
const int SIZEOF_ANSWERS = 8;
string magicEightBallAnswers[SIZEOF_ANSWERS] = { "Yes", "No", "Maybe", "It's not certain", "The outlook is good",
"The outlook is poor", "Time will tell", "Most likely" };
int main(int argc, char *argv[])
{
bool keepGoing = true;
while (keepGoing)
{
string question;
//prompt for and get the question
cout << "What is your question? (Enter 'x' to exit)" << endl;
getline(cin, question);
//this assumes that the user enters a lower case x
if (question.compare(exitString) == 0)
keepGoing = false;
else
{
cout << getAnswer() << endl;
}
}
return 0;
}
string getAnswer()
{
int index = rand() % SIZEOF_ANSWERS;
return magicEightBallAnswers[index];
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
