Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Can someone please help me with this code? I'm writing in C++. Thank you in advance. Complete a program that represents a Magic Eight Ball

Can someone please help me with this code? I'm writing in C++. Thank you in advance.

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"

Now here is the program we need to complete, Instructions for what to do to complete it are in the file as comments.

#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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Organic Chemistry

Authors: Joseph M. Hornback

2nd Edition

9781133384847, 9780199270293, 534389511, 1133384846, 978-0534389512