Write a program that simulates a Magic 8-Ball. Prompt the user for a question, and randomly select a response from a vector. The attached Responses file is available if you want to use it, OR you may create your own response file, OR you can add/modify/remove the responses in the attached file. Continue to prompt for questions until the user wants to exit the program (Pick your own exit condition, but the program must loop until the condition is met!).Additional requirements:Header comments must be presentPrototypes must be present if functions are usedHello and goodbye messages must be shownUse vector(s) for implementationUse comments and good style practicesHINT: Read the contents of the response file into an a vector. When the user enters a question, generate a random index and display the response from the vector with that index. Make sure to check for the exit condition.To generate the random index, use the following in main() - this should be familiar by now...unsigned seed = time(0);srand(seed);Make sure to include all the necessary header files (cstdlib, ctime, vector, etc.).If you use the provided response file, download the attached text file and place in same location as your program.If you create your own response file, make sure to place it in the same location as your program.SAMPLE RUN:Welcome to the Magic 8-Ball program!Ask yes/no questions and the program will give you clarity about the outcome...Enter EXIT to exit the program.Please ask your question: Will I get a good grade in this class?Without a doubt, yes.Please ask your question: Does the professor know what he's talking about?I'll tell you after my nap.Please ask your question: EXITExiting program. Goodbye! Language is C++Please make it as simple as possible.

Question
Asked Nov 20, 2019
82 views

Write a program that simulates a Magic 8-Ball. Prompt the user for a question, and randomly select a response from a vector. The attached Responses file is available if you want to use it, OR you may create your own response file, OR you can add/modify/remove the responses in the attached file. Continue to prompt for questions until the user wants to exit the program (Pick your own exit condition, but the program must loop until the condition is met!).

Additional requirements:

  • Header comments must be present
  • Prototypes must be present if functions are used
  • Hello and goodbye messages must be shown
  • Use vector(s) for implementation
  • Use comments and good style practices

HINT: Read the contents of the response file into an a vector. When the user enters a question, generate a random index and display the response from the vector with that index. Make sure to check for the exit condition.

To generate the random index, use the following in main() - this should be familiar by now...unsigned seed = time(0);

srand(seed);

Make sure to include all the necessary header files (cstdlib, ctime, vector, etc.).If you use the provided response file, download the attached text file and place in same location as your program.

If you create your own response file, make sure to place it in the same location as your program.

SAMPLE RUN:Welcome to the Magic 8-Ball program!Ask yes/no questions and the program will give you clarity about the outcome...Enter EXIT to exit the program.
Please ask your question: Will I get a good grade in this class?Without a doubt, yes.
Please ask your question: Does the professor know what he's talking about?I'll tell you after my nap.
Please ask your question: EXIT

Exiting program. Goodbye!

 

Language is C++

Please make it as simple as possible.

check_circle

Expert Answer

Step 1

Make program and response.txt file in same directory (folder).

  • do while loop is used to continue to prompt for questions until the user wants to exit the program.
  • getline() function is used to input question from user.
Step 2

Program:

//header file include for console input and output

#include <iostream>

//header file include for srand function

#include<cstdlib>

//header file include time function

#include<ctime>

//header file include for vector

#include<vector>

//header file include for file input

#include<fstream>

//namespace inclusion

using namespace std;

//main method

int main()

{

    string question,answer;

    //Hello message

    cout<<"Hello, Welcome to the Magic 8-Ball program!Ask yes/no questions and the program will give you clarity about the outcome...Enter EXIT to exit the program."<<endl;

    //do while loop

    do{

        //message to user

        cout<<"Please ask your question:"<<endl;

        //Prompt the user for a question

        getline(cin, question);

        //declare vector

        vector<string> response;

        //input file

        ifstream file("response.txt");

        //if file is unable to open

        if(!file.is_open())

            {

                cout<<"Error while opning file"<<endl;

            }

        //read lines from file

        while(getline(file,answer))

            {

                //push_back lines in vector

                response.push_back(answer);

               

            }

            //generate the random index

            unsigned seed=time(0);

            srand(seed);

            int index=(rand()%12);

            if(question=="exit")

                break;

            //randomly select a response from a vector

            cout<<response[index]<<endl;

        //closing of file

        file.close();

        }

        //do while condition

        while(question!="exit");

        //goodbye message

    cout<<"Exiting program. Goodbye!"<<endl;

    //return to console

    return 0;

}

Step 3

Response.txt contains:...

help_outline

Image Transcriptionclose

Yes, of Course! Without a doubt, Yes You can count on it. For Sure Ask me Later I'm not sure I can't tell you right now I'11 tell you after my nap No way I don't think so Without a doubt, no The answer is clearly NO.

fullscreen

Want to see the full answer?

See Solution

Check out a sample Q&A here.

Want to see this answer and more?

Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*

See Solution
*Response times may vary by subject and question.
Tagged in

Engineering

Computer Science

Related Computer Science Q&A

Find answers to questions asked by student like you
Show more Q&A
add
question_answer

Q: Write a C++ program: you are now working for an accounting firm. Your employer is interested in dete...

A: This program accepts the number of hours and pay rate of an employee as input.For 40 regular hours a...

question_answer

Q: What are the four components of php application and explain why these are so important?

A: Four components of a PHP application are the following:DatabaseHTMLSessions/Authentication/ CookiesU...

question_answer

Q: what are the major differences between php and asp.net?

A: PHP stands for PHP Hypertext Preprocessor, it is server-side scripting used to make interactive and ...

question_answer

Q: In MS Access, to ensure that a promised date valued entered must a future point in time, whichvalida...

A: Now() function:The Now() function in Microsoft Access is used to return current date and time based ...

question_answer

Q: What is single loss expectancy? What is annualized loss expectancy? What is wrong with these risk me...

A: Single loss expectancy is related to risk management and is considered as the value related to money...

question_answer

Q: I have written a code following the instructions below, but the choice 2, convert Fahrenheit to Cels...

A: Reason for not asking the user to enter another value:In the code given in the question, if the choi...

question_answer

Q: WHAT ARE THE STAKEHOLDERS OF BITCOIN VALUE CHAIN?

A: The stakeholders in bitcoin value chain are:Developers Developers play an important role as they use...

question_answer

Q: Using the data file capabilities of C++, create a Personnel file, personnel.dat on your drive and th...

A: Program Instructions:Use do-while loop to repeatedly present the menu.Use switch statement to opt di...

question_answer

Q: PLEASE EXPLAIN ALL STEPS  In the following code, determine the values of the symbols this, that, and...

A: Before explaining the difference first user must know about the Assembly language.Assembly language:...