We need source control. having to keep pushing commits on a post by post basis inflates this thread.
Code
#include <iostream>
#include <string>
using namespace std;
class Question
{
public:
Question(){}
Question(string text, string choice1, string choice2, string choice3, string choice4, int answer)
{
setText(text);
setAnswer(answer);
setChoices(choice1,choice2,choice3,choice4);
}
~Question(){ }
static const int nchoices = 4;
void display();
bool judge(int);
string getText();
int getAnswer();
void setText(string);
void setAnswer(int);
void setChoices(string,string,string,string);
private:
string _text;
string _choice1,_choice2,_choice3,_choice4;
int _answer;
};
void Question::display()
{
cout << _text << endl;
cout << "1)" << _choice1 << endl;
cout << "2)" << _choice2 << endl;
cout << "3)" << _choice3 << endl;
cout << "4)" << _choice4 << endl;
}
bool Question::judge(int guess)
{
return getAnswer() == guess;
}
string Question::getText()
{
return _text;
}
int Question::getAnswer()
{
return _answer;
}
void Question::setText(string text)
{
_text = text;
}
void Question::setAnswer(int answer)
{
_answer = answer;
}
void Question::setChoices(string choice1, string choice2, string choice3, string choice4)
{
_choice1 = choice1;
_choice2 = choice2;
_choice3 = choice3;
_choice4 = choice4;
}
class Player
{
public:
Player(){}
Player(string name) : _name(name) {_points = 0;}
~Player(){}
void display();
string getName();
int getPoints();
void setName(string);
void addPoint();
private:
string _name;
int _points;
};
void Player::display()
{
cout << getName() << " has " << getPoints() << " point(s)." << endl;
}
string Player::getName()
{
return _name;
}
int Player::getPoints()
{
return _points;
}
void Player::setName(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 << "Enter 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::playGame()
{
int answer;
for(int i = 0, j = 0; j < _nquestions; i= (++i)%_nplayers, j++)
{
cout << _players[i].getName() << " answer the following question:" << endl;
_questions[j].display();
cin >> answer;
if(_questions[j].judge(answer))
_players[i].addPoint();
}
showWinner();
}
void Game::showWinner()
{
int winner, maxPoints = 0;
for(int i = 0; i < _nplayers; i++)
{
_players[i].display();
if(_players[i].getPoints() > maxPoints)
{
maxPoints = _players[i].getPoints();
winner = i;
}
}
cout << "The winner is " << _players[winner].getName() << endl;
}
int main()
{
Game* g = new Game();
g->playGame();
delete g;
}