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!).
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);
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
A C++ program for the given criteria is as follows,
* @ file main.cpp
* @brief This is the program that simulates a Magic 8-Ball
//Declare header files
using namespace std;
//Declare a function
//Declare necessary constant variables
const string exitString = "EXIT";
const int SIZEOF_ANSWERS = 12;
//Define the method
bool getFileContent(string file_Name, vector<string> & vecOf_Strs)
// Open the File
//Check if object is valid
cerr << "Cannot open the File : "<<file_Name<<endl;
// Read the next line from File untill it reaches the end.
while (getline(in, str))
// Line contains string of length > 0 then save it in vector
if(str.size() > 0)
//Close the File
//Define the main() method
int main(int argc, char *argv)
bool keepGoing = true;
cout<<"Welcome to the Magic 8-Ball program!Ask yes/no questions"<<endl;
cout<<"The program will give you clarity about the outcome..."<<endl;
cout<<"Enter EXIT to exit the program."<<endl;
//Prompt the user to get the question
cout << "Please ask your question: " << endl;
//Check whether the enered string matches "EXIT"
if (question.compare(exitString) == 0)
keepGoing = false;
cout << getAnswer() << endl;
//Define the method
//Generate the random index
int index = rand() % SIZEOF_ANSWERS;
// Get the contents of file in a vector
bool result = getFileContent("response.txt", vecOf_Str);
//Return the text of random index
Screenshot of program #1:
Screenshot of program #2:
Screenshot of program #3...
Solutions are written by subject experts who are available 24/7. Questions are typically answered within 1 hour.*See Solution
Q: Too Many Networks While attempting to connect to the wireless network at your job, you notice that f...
A: Selecting the appropriate wireless network:The user can determine the wireless network to which he/s...
Q: construct a pda that accepts the language defined by the grammar s → abSb|λ.
A: Constructing PDA:There are two states q and f. q is the start state and f is the final state. Here i...
Q: Write a C++ program that computes a student’s grade for an assignment as a percentage given the stud...
A: Instructions for the given program:For the given program, there should exist a file with the name “R...
Q: d. Explain the steps of the fetch-decode-execute cycle. Your explanation should include what is happ...
A: A program consists of collections of instructions encoded using the method of binary numbers. The fe...
Q: ABC Accounting Company is a company that manages the financials for various small companies on a nat...
A: 1) To prevent remote employees from being hacked, the following steps can be taken:A Virtual private...
Q: Based on the given block data below, answer the following questions. Hint: First do Google search fo...
A: Q1. (Layer 4) Protocol is:Based on the above input data block result, the Protocol used in layer 4 (...
Q: WHAT IS BLOCKCHAIN?
A: DefinitionBlockchain is a novel technology of today’s era. Blockchain is basically a growing list of...
Q: Write a program in Python that converts an improper fraction to a mixed number. The program should p...
A: Program:#Display the messageprint("Calculate Mixed Number")print("======================")print()#Pr...
Q: 4. Explain the steps of the fetch-decode-execute cycle. Your explanation should include what is happ...
A: Registers that are involved in the instruction cycle are as follows:Memory Address Registers (MAR): ...