Question: Need Help with the following C++ Program: Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions

Need Help with the following C++ Program:

Complete a program that represents a Magic Eight Ball (a Magic Eight Ball allows you to ask questions and receive one of several random answers).

In order to complete this, you will need a couple of new functions.

First, in order to get a line of input that can contain spaces, you cannot use cin, but instead will use getline:

string question;

cout << "What is your question? (Enter 'x' to exit)" << endl; getline(cin, question);

Second, an alternative to using == to compare two strings is the string compare function. We'll look later at reasons to use this, but

if (question.compare("x") == 0)

//found an "x"

Following code is provided to build program off of:

#include #include #include using namespace std; string getAnswer(string m8Ball[], int nAnswers); int main() { //define a constant that represents how many answers are in your array (at least 8) //declare and initialize an array of strings, representing possible answers from the magic eightball srand((unsigned int) time(NULL)); //loop and ask the user to enter a question or enter "x" to stop //use getline to get the question //call getAnswer with your array and number of possible answers to get an answer //output the answer } string getAnswer(string m8Ball[], int nAnswers) { int index = rand() % nAnswers; return m8Ball[index]; }

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!