In C++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 goodbye!

Question
Asked Nov 23, 2019
29 views

In C++

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 goodbye!

check_circle

Expert Answer

Step 1

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

#include <iostream>

#include <string>

#include <iomanip>

#include <string>

#include <stdio.h>

#include <fstream>

#include <stdio.h>

#include<vector>

//Declare namespace

using namespace std;

//Declare a function

string getAnswer();

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

    ifstream in(file_Name.c_str());

            //Check if object is valid

            if(!in)

            {

                        cerr << "Cannot open the File : "<<file_Name<<endl;

                        return false;

            }

            string str;

            // 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)

                                    vecOf_Strs.push_back(str);

            }

            //Close the File

            in.close();

            return true;

}

//Define the main() method

int main(int argc, char *argv[])

{

    bool keepGoing = true;

    //Print heading

    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;

    while (keepGoing)

    {

        string question;

        //Prompt the user to get the question

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

        getline(cin, question);

        //Check whether the enered string matches "EXIT"

        if (question.compare(exitString) == 0)

        {

            cout<<"Exiting goodbye!";

            keepGoing = false;

        }

        else

        {

            cout << getAnswer() << endl;

        }

    }

    return 0;

}

//Define the method

string getAnswer()

{

    //Generate the random index

    int index = rand() % SIZEOF_ANSWERS;

    vector<string> vecOf_Str;

            // Get the contents of file in a vector

            bool result = getFileContent("response.txt", vecOf_Str);

    //Return the text of random index

    return vecOf_Str[index];

}

Screenshot of program #1:

@ file main.cpp
* @brief This is the program that simulates a Magic 8-Ball
*/
/Declare header files
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
include <stdio.h>
#include <fstream>
#include <stdio.h>
#include<vector
//Declare namespace
using namespace std;
//Declare a function
string getAnswer )
//Declare necessary constant variables
const string exitString = "EXIT"
const int SIZEOF ANSWERS = 12;
help_outline

Image Transcriptionclose

@ file main.cpp * @brief This is the program that simulates a Magic 8-Ball */ /Declare header files #include <iostream> #include <string> #include <iomanip> #include <string> include <stdio.h> #include <fstream> #include <stdio.h> #include<vector //Declare namespace using namespace std; //Declare a function string getAnswer ) //Declare necessary constant variables const string exitString = "EXIT" const int SIZEOF ANSWERS = 12;

fullscreen
Step 2

Screenshot of program #2:

/Define the method
bool getFileContent (string file Name, vector<string> & vecof Strs)
7Open the File
ifstream in (file Name.c str))
//Check if object is valid
if(!in)
"<<file Name<<endl
cerr < "Cannot open the File :
return false;
string str
/Read the next line from File untilil it reaches the end
while (getline (in, str))
/Line contains string of length > 0 then save it in vector
if (str.size () > 0)
vecof_Strs.push_back (str);
//close the File
in.close )
return true
help_outline

Image Transcriptionclose

/Define the method bool getFileContent (string file Name, vector<string> & vecof Strs) 7Open the File ifstream in (file Name.c str)) //Check if object is valid if(!in) "<<file Name<<endl cerr < "Cannot open the File : return false; string str /Read the next line from File untilil it reaches the end while (getline (in, str)) /Line contains string of length > 0 then save it in vector if (str.size () > 0) vecof_Strs.push_back (str); //close the File in.close ) return true

fullscreen
Step 3

Screenshot of program #3...

Define the main ( method
int main (int argc, char argv[]
bool keepGoing
= true
//Print heading
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;
while (keepGoing)
string question
/Prompt the user to get the question
cout < "Please ask your question:
" << endl
getline (cin, question) ;
//Check whether the enered string matches "EXIT"
if (question.compare (exitString)
cout<<"Exiting goodbye!"
keepGoing = false
help_outline

Image Transcriptionclose

Define the main ( method int main (int argc, char argv[] bool keepGoing = true //Print heading 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; while (keepGoing) string question /Prompt the user to get the question cout < "Please ask your question: " << endl getline (cin, question) ; //Check whether the enered string matches "EXIT" if (question.compare (exitString) cout<<"Exiting goodbye!" keepGoing = false

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: 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...

question_answer

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

question_answer

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

question_answer

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

question_answer

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

question_answer

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 (...

question_answer

Q: WHAT IS BLOCKCHAIN?

A: DefinitionBlockchain is a novel technology of today’s era. Blockchain is basically a growing list of...

question_answer

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

question_answer

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): ...