Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

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_2

Step: 3

blur-text-image_3

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

Fundamentals Of Database Systems

Authors: Ramez Elmasri, Shamkant B. Navathe

7th Edition Global Edition

1292097612, 978-1292097619

More Books

Students also viewed these Databases questions

Question

What is the background of the situation?

Answered: 1 week ago