Question

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

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 <iostream>

#include <string>

#include <ctime>

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];

}

0 0
Add a comment Improve this question Transcribed image text
Answer #1

HI, Please find my implementation.

Please let me know in case of any issue.

#include <iostream>
#include <string>
#include <ctime>

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)
const int NUM_OF_ANS = 8;
//declare and initialize an array of strings, representing possible answers from the magic eightball
string m8Ball[] = {"A", "B", "C", "D", "E", "F", "G", "H"};

srand((unsigned int) time(NULL));

    string question;
//loop and ask the user to enter a question or enter "x" to stop
while(true){
   //use getline to get the question
       cout << "What is your question? (Enter 'x' to exit)" << endl;
       getline(cin, question);

       if (question.compare("x") == 0)
           break;
  
   //call getAnswer with your array and number of possible answers to get an answer
   //output the answer
   cout<<"Answer: "<<getAnswer(m8Ball, NUM_OF_ANS)<<endl;
}

}

string getAnswer(string m8Ball[], int nAnswers)
{
int index = rand() % nAnswers;

return m8Ball[index];
}

thì rdweek thirdweek > thirdweek g++ m8Ball.cpp thì rdweek ./a.out What is your question? (Enter x to exit) What is first l

Add a comment
Know the answer?
Add Answer to:
Can someone please help me with this code? I'm writing in C++. Thank you in advance....
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • (C++) This is the part 2 of the program. In this program you will use part...

    (C++) This is the part 2 of the program. In this program you will use part 1 and build on it to call getAnswer function which would return a random answer. Remember that you had stored these random answers in a string array. You will pass the array and the possible answers (ideally the size of the array) to getAnswer, and the getAnswer will return a randomly selected answer back to main. This is the starting code (below). Please make...

  • C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will...

    C++ programming question, please help! Thank you so much in advance!!! In this exercise, you will work with 2 classes to be used in a RPG videogame. The first class is the class Character. The Character class has two string type properties: name and race. The Character class also has the following methods: a constructor Character(string Name, string Race), that will set the values for the name and the race variables set/get functions for the two attributes a function print(),...

  • I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7:...

    I need help with this code. I'm using C++ and Myprogramming lab to execute it. 11.7: Customer Accounts Write a program that uses a structure to store the following data about a customer account:      Customer name      Customer address      City      State      ZIP code      Telephone      Account balance      Date of last payment The program should use an array of at least 20 structures. It should let the user enter data into the array, change the contents of any element, and display all the...

  • May i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #...

    may i ask for help with this c++ problem? this is the code i have for assignment 4 question 2: #include<iostream> #include<string> #include<sstream> #include<stack> using namespace std; int main() { string inputStr; stack <int> numberStack; cout<<"Enter your expression::"; getline(cin,inputStr); int len=inputStr.length(); stringstream inputStream(inputStr); string word; int val,num1,num2; while (inputStream >> word) { //cout << word << endl; if(word[0] != '+'&& word[0] != '-' && word[0] != '*') { val=stoi(word); numberStack.push(val); // cout<<"Val:"<<val<<endl; } else if(word[0]=='+') { num1=numberStack.top(); numberStack.pop(); num2=numberStack.top(); numberStack.pop();...

  • C++ getline errors I am getting getline is undefined error messages. I would like the variables...

    C++ getline errors I am getting getline is undefined error messages. I would like the variables to remain as strings. Below is my code. #include <iostream> #include<string.h> using namespace std; int index = 0; // variable to hold how many customers are entered struct Address //Structure for the address. { int street; int city; int state; int zipcode; }; // Customer structure struct Customer { string firstNm, lastNm; Address busAddr, homeAddr; }; // Functions int displayMenu(); Customer getCustomer(); void showCustomer(Customer);...

  • Can someone please tell me why the calculation for earnings is not coming through at the...

    Can someone please tell me why the calculation for earnings is not coming through at the end of the program? Thank you. //This program should tell us about streaming services #include <iostream> #include <iomanip> #include <string> using namespace std; int main() {    int choice, streams;    double earnings;    string song;    string artist;    string streamingService;       const int Tidal = 0.01250;    const int Amazon = 0.00402;    const int Apple_Music = 0.00735;    const int...

  • Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the...

    Hi this is C++, I'm really struggle with it please help me.... ************************ Here is the original high score program,: Write a program that records high-score data for a fictitious game. The program will ask the user to enter five names, and five scores. It will store the data in memory, and print it back out sorted by score. The output from your program should look exactly like this: Enter the name for score #1: Suzy Enter the score for...

  • Can someone help me with the writing of this code please? public static int promptInt(Scanner input,...

    Can someone help me with the writing of this code please? public static int promptInt(Scanner input, String prompt, int min, int max) { return 0; //TODO replace }    /**    * Returns the index within arr of the first occurrence of the specified character.    * If arr is null or 0 length then return -1. For all arrays, don't assume a length    * but use the array .length attribute.    * @param arr The array to look...

  • In this module you learned about File Handling. You began learning about how data can be...

    In this module you learned about File Handling. You began learning about how data can be imported into, manipulated in, and exported from a program. Alter the assignment from Ch6 (Magic 8 Ball Game) so that it reads the Magic 8 Ball game sayings from a file and loads them into an array. The rest of the program should be the same. Previous Game: "The magic ball eight was created in the 50's and was produced by mattel. Eight Ball...

  • In this module you learned about arrays. You also began learning about implementing arrays and how...

    In this module you learned about arrays. You also began learning about implementing arrays and how they can help organize the data in a program. The magic ball eight was created in the 50's and was produced by mattel. Eight Ball is a toy and is just a game. The magic 8 ball answers your yes/no questions. You will be creating a version of the Magic 8 Ball game. You can use whats inside the attached file to build your...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT