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

From Starting Out with C++ (8th Edition) programming challenge chapter 13 number

ID: 3874899 • Letter: F

Question

From Starting Out with C++ (8th Edition) programming challenge chapter 13 number 19 PC

Program 2:

In this programming challenge you will create a simple trivia game for two players. The program will work like this:

Starting with player 1, each player gets a turn at answering five trivia questions. (There are a total of 10 questions.) When a question is displayed, four possible answers are also displayed. Only one of the answers is correct, and if the player selects the correct answer he or she earns a point.

After answers have been selected for all of the questions, the program displays the number of points earned by each player and declares the player with the highest number of points the winner.

In this program you will design a Question class to hold the data for a trivia question. The Question class should have member variables for the following data:

A trivia question

Possible answer #1

Possible answer #2

Possible answer #3

Possible answer #4

The number of the correct answer (1, 2, 3, or 4)

The Question class should have appropriate constructor(s), accessor, and mutator functions.

The program should create an array of 10 Question objects, one for each trivia ques- tion. Make up your own trivia questions on the subject or subjects of your choice for the objects.

The program should have a menu for each question with the possible answers as shown here:

Question Bank for XYZ Subject
--------------------------------------------------------------

Question for PLAYER 1:
----------------------
(1) What famous document begins: "When in the course of human events..."?
ANSWERS:
1. The Gettysburg Address
2. The US Declaration of Independence
3. The Magna Carta
4. The US Bill of Rights
Enter the correct answer: 1
Sorry, that's incorrect. The correct answer is 2. The US Declaration of Independence

Question for PLAYER 2:
----------------------
(2) Who said "A billion dollars isn't worth what it used to be"?
ANSWERS:
1. J. Paul Getty
2. Bill Gates
3. Warren Buffet
4. Henry Ford
Enter the correct answer: 1
Correct!

Your program does not need to be a replica, but it should be representative of the output that is shown here. Use a loop to display the questions. You may hardcode the question/answer data, but try to read it from a file.

Explanation / Answer

#include <iostream>
#include <string>

using namespace std;

class Question
{
public:
Question(){}
Question(string text, string choiceone, string choicetwo, string choicethree, string choicefour, int answer)
{
letssetText(text);
letssetAnswer(answer);
letssetChoices(choice1,choice2,choice3,choice4);
}
~Question(){ }
static const int nchoices = 4; // number of choices is 4
void display();
bool judge(int);
string getText();
int getAnswer();
void letssetText(string);
void letssetAnswer(int);
void letssetChoices(string,string,string,string);
private:
string _text;
string _choiceone,_choicetwo,_choicethree,_choicefour;
int _answer;

};

void Question::display()
{
cout << _text << endl;
cout << "1)" << _choiceone << endl;
cout << "2)" << _choicetwo << endl;
cout << "3)" << _choicethree << endl;
cout << "4)" << _choicefour << endl;
}

bool Question::judge(int guess)
{
return getAnswer() == guess;
}

string Question::getText()
{
return _text;
}

int Question::getAnswer()
{
return _answer;
}

void Question::letssetText(string text)
{
_text = text;
}

void Question::letssetAnswer(int answer)
{
_answer = answer;
}

void Question::letssetChoices(string choiceone, string choicetwo, string choicethree, string choicefour)
{
_choiceone = choiceone;
_choicetwo = choicetwo;
_choicethree = choicethree;
_choicefour= choicefour;
}



class Player
{

public:
Player(){}
Player(string name) : _name(name) {_points = 0;}
~Player(){}
void display();
string getplayerName();
int getthePoints();
void letssetName(string);
void addPoint();
private:
string _name;
int _points;
};

void Player::display()
{
cout << getplayerName() << " has " << getthePoints() << " point(s)." << endl;
}

string Player::getplayerName()
{
return _name;
}

int Player::getthePoints()
{
return _points;
}

void Player::letssetName(string name)
{
_name = name;
}

void Player::addPoint()
{
_points++;
}



class Game
{
public:
Game()
{
createPlayers();
createQuestions();
}
~Game(){ }
void createPlayers();
void createQuestions();
void playGame();
void showWinner();
private:
static const int _nplayers = 2;
static const int _nquestions = 10;
Player _players[_nplayers];
Question _questions[_nquestions];

};

void Game::createPlayers()
{
string name;

for(int i = 0; i < _nplayers; i++)
{
cout << "Enter the name of player " << (i+1) << ": ";
cin >> name;
_players[i] = Player(name);
}
}

void Game::createQuestions()
{
int answer;
string text;
string choices[Question::nchoices];


for(int i = 0; i < _nquestions; i++)
{
cout << " pleaseEnter a question:";
cin >> text;
for(int j = 0; j < Question::nchoices; j++)
{
cout << "Enter choice " << (j+1) << " of " << Question::nchoices << ":";
cin >> choices[j];
}

cout << "Enter the choice number that is the answer:";
cin >> answer;


_questions[i] = Question(text,choices[0],choices[1],choices[2],choices[3],answer);
}
}

void Game::letsplayGame()
{
int answer;
for(int i = 0, j = 0; j < _nquestions; i= (++i)%_nplayers, j++)
{
cout << _players[i].getplayerName() << " answer the following question:" << endl;
_questions[j].display();
cin >> answer;

if(_questions[j].judge(answer))
_players[i].addPoint();

}

nowshowWinner();
}

void Game::nowshowWinner()
{
int winner, maxPoints = 0;
for(int i = 0; i < _nplayers; i++)
{
_players[i].display();

if(_players[i].getthePoints() > maxPoints)
{
maxPoints = _players[i].getthePoints();
winner = i;
}
}

cout << "The winner is " << _players[winner].getplayerName() << endl;

}



int main()
{
Game* m = new Game();

m->playGame();

delete m;

}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote