Convert the magic eight ball program from the arrays module to use a vector inst
ID: 3817454 • Letter: C
Question
Convert the magic eight ball program from the arrays module to use a vector instead of an array. Be sure to do these things in your program:
use the push_back() function to initialize the array
modify the signature and prototype of the getAnswer() function
modify the code in the body of the getAnswer() function
remove any unneeded code, such as your constant for the number of answers
The program is below:
/*Sample program to representa magic eight ball using a string array.
#include <iostream>
#include <string>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <fstream>
#include <stdio.h>
using namespace std;
string getAnswer();
const string exitString = "x";
const int SIZEOF_ANSWERS = 8;
string magicEightBallAnswers[SIZEOF_ANSWERS] = { "Yes", "No", "Maybe", "It's notcertain", "The outlook is good", "The outlook is poor", "Timewill tell", "Most likely" };
int main(int argc, char *argv[]){
bool keepGoing = true;
while (keepGoing){string question;
//prompt for and get the question
cout << "What is your question? (Enter 'x' to exit)" << endl;
getline(cin, question);
//this assumes that the user enters a lower case xif (question.compare(exitString) == 0)
keepGoing = false;
else{
cout << getAnswer() << endl;
}
}return 0;
}
string getAnswer(){
int index = rand() % SIZEOF_ANSWERS;return magicEightBallAnswers[index];
}
Please help me with this C++ program. Thank you in advance.
Explanation / Answer
#include <bits/stdc++.h>
#include <string>
#include <iomanip>
#include <string>
#include <stdio.h>
#include <fstream>
#include <stdio.h>
using namespace std;
string getAnswer(vector<string> &v);
const string exitString = "x";
int main(int argc, char *argv[])
{
std::vector<string> v;
bool keepGoing = true;
v.push_back("Yes");
v.push_back("No");
v.push_back("Maybe");
v.push_back("It's notcertain");
v.push_back("The outlook is good");
v.push_back("The outlook is poor");
v.push_back("Timewill tell");
v.push_back("Most likely");
while (keepGoing)
{
string question;
//prompt for and get the question
cout << "What is your question? (Enter 'x' to exit)" << endl;
getline(cin, question);
//this assumes that the user enters a lower case x
if (question.compare(exitString) == 0)
keepGoing = false;
else{
cout << getAnswer(v) << endl;
}
}return 0;
}
string getAnswer(vector<string>&v)
{
int index = rand() % v.size();
return v[index];
}
========================================================================
Output:
akshay@akshay-Inspiron-3537:~/Chegg$ g++ veee.cpp
akshay@akshay-Inspiron-3537:~/Chegg$ ./a.out
What is your question? (Enter 'x' to exit)
how are you?
Most likely
What is your question? (Enter 'x' to exit)
What is your name?
Timewill tell
What is your question? (Enter 'x' to exit)
Do you like apple?
No
What is your question? (Enter 'x' to exit)
x
===================================================================
Please rate my answer that will be great help