Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

USE METHODS instead of Main in this 8-ball code. So, all of the code should most

ID: 3542401 • Letter: U

Question

USE METHODS instead of Main in this 8-ball code. So, all of the code should mostly be in methods, not in the main. The program is written using the code under                    main, but change them so methods are used instead.

Program:

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
int main()
{
    int randNum;
    srand (time(NULL));
    randNum = rand() % 20 + 1;

    // header print
    cout << "Magic 8 Ball ";
    string response; // declare variable to get response
    cout <<"Do you want to ask a question(yes/no)? ";
    cin >> response;
    cout << endl;

    // if response is yes proceed.
    if(response.compare("yes")==0)
    {
        string question;// declare variable to get question
        cout << "What is your question? ";
        cin.ignore();
        
        //read question from standard input/
        getline(cin,question);
        cout << endl;

        // if question length is more than 60. print message and exit program
        if(question.length()>=60)
        {
            cout <<"Your question is too long. Be more concise. Goodbye ";
            return 0;
        }
        else
        {
        // else print question
        printf("Question: %s ",question.c_str());
        switch(randNum)
        {
// now based on randNum print the Answer message.
case 1:printf("Answer: %s ","Signs point to yes."); break;
case 2:printf("Answer: %s ","Yes."); break;
case 3:printf("Answer: %s ","Reply hazy, try again."); break;
case 4:printf("Answer: %s ","Without a doubt."); break;
case 5:printf("Answer: %s ","My sources say no."); break;
case 6:printf("Answer: %s ","As I see it, yes."); break;
case 7:printf("Answer: %s ","You may rely on it."); break;
case 8:printf("Answer: %s ","Concentrate and ask again."); break;
case 9:printf("Answer: %s ","Outlook not so good."); break;
case 10:printf("Answer: %s ","It is decidedly so."); break;
case 11:printf("Answer: %s ","Better not tell you now.."); break;
case 12:printf("Answer: %s ","Very doubtful."); break;
case 13:printf("Answer: %s ","Yes - definitely."); break;
case 14:printf("Answer: %s ","It is certain."); break;
case 15:printf("Answer: %s ","Cannot predict now."); break;
case 16:printf("Answer: %s ","Most likely.."); break;
case 17:printf("Answer: %s ","Ask again later."); break;
case 18:printf("Answer: %s ","My reply is no."); break;
case 19:printf("Answer: %s ","Outlook good."); break;
case 20:printf("Answer: %s ","Don't count on it."); break;
}
}
}
// else say good bye and exit.
else
{
    cout << "Goodbye ";
    return 0;
}
return 0;
}

Explanation / Answer

#include <iostream>
#include <cstdlib>
#include <time.h>
using namespace std;
void get_question()
{
string question;// declare variable to get question
cout << "What is your question? ";
cin.ignore();

//read question from standard input/
getline(cin,question);
cout << endl;

// if question length is more than 60. print message and exit program
if(question.length()>=60)
{
cout <<"Your question is too long. Be more concise. Goodbye ";
return 0;
}
else
{
// else print question
printf("Question: %s ",question.c_str());
switch(randNum)
{
// now based on randNum print the Answer message.
case 1:printf("Answer: %s ","Signs point to yes."); break;
case 2:printf("Answer: %s ","Yes."); break;
case 3:printf("Answer: %s ","Reply hazy, try again."); break;
case 4:printf("Answer: %s ","Without a doubt."); break;
case 5:printf("Answer: %s ","My sources say no."); break;
case 6:printf("Answer: %s ","As I see it, yes."); break;
case 7:printf("Answer: %s ","You may rely on it."); break;
case 8:printf("Answer: %s ","Concentrate and ask again."); break;
case 9:printf("Answer: %s ","Outlook not so good."); break;
case 10:printf("Answer: %s ","It is decidedly so."); break;
case 11:printf("Answer: %s ","Better not tell you now.."); break;
case 12:printf("Answer: %s ","Very doubtful."); break;
case 13:printf("Answer: %s ","Yes - definitely."); break;
case 14:printf("Answer: %s ","It is certain."); break;
case 15:printf("Answer: %s ","Cannot predict now."); break;
case 16:printf("Answer: %s ","Most likely.."); break;
case 17:printf("Answer: %s ","Ask again later."); break;
case 18:printf("Answer: %s ","My reply is no."); break;
case 19:printf("Answer: %s ","Outlook good."); break;
case 20:printf("Answer: %s ","Don't count on it."); break;
}
}
}
int main()
{
int randNum;
srand (time(NULL));
randNum = rand() % 20 + 1;
// header print
cout << "Magic 8 Ball ";
string response; // declare variable to get response
cout <<"Do you want to ask a question(yes/no)? ";
cin >> response;
cout << endl;
// if response is yes proceed.
if(response.compare("yes")==0)
{
get_question();
}
// else say good bye and exit.
else
{
cout << "Goodbye ";
return 0;
}
return 0;
}