d2jsp
Log InRegister
d2jsp Forums > Off-Topic > Computers & IT > Programming & Development > 500 Fg For C++ Homework Help
12Next
Add Reply New Topic New Poll
Member
Posts: 9,624
Joined: Sep 7 2008
Gold: 3,272.00
May 5 2014 07:37pm
I'm not going to go into detail but I am unable to do my homework, so I am going to pay 500fg to write this simple code.
Welcome to Pm or post, and I need this before 2 days time.
If you believe 500fg is not enough for this let me know VIA pm and we can work something out

~ Info ~
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 variable 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 constructors(), accessor, and mutator functions.

The program should create an array of 10 Question objects, one for each trivia question. make up your own trivia questions on the subject or subjects of your choice for the objects.
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 5 2014 07:49pm
C, C++ or C#?
Member
Posts: 32,925
Joined: Jul 23 2006
Gold: 3,804.50
May 5 2014 07:51pm
Quote (Minkomonster @ May 5 2014 09:49pm)
C, C++ or C#?


i'm going to take a wild guess

Quote
500 Fg For C++ Homework Help


;)

This post was edited by carteblanche on May 5 2014 07:51pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 5 2014 07:56pm
Didn't see it lol
Member
Posts: 9,624
Joined: Sep 7 2008
Gold: 3,272.00
May 5 2014 07:58pm
Haha sorry, I should have repeated in body text
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 5 2014 10:43pm
Code
#include <iostream>
#include <stdlib.h>
#include <time.h>

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(){ }
void display();
static int getNumberOfChoices();
bool judge(int);
string getText();
int getAnswer();
void setText(string);
void setAnswer(int);
void setChoices(string,string,string,string);
private:
static const int _nchoices = 4;
string _text;
string _choice1,_choice2,_choice3,_choice4;
int _answer;

};

int Question::getNumberOfChoices()
{
return _nchoices;
}

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;
static const int _nasked = 5;
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, nchoices = Question::getNumberOfChoices();
string text;
string choices[nchoices];


for(int i = 0; i < _nquestions; i++)
{
cout << "Enter a question:";
cin >> text;
for(int j = 0; j < nchoices; j++)
{
cout << "Enter choice " << (j+1) << " of " << 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()
{
srand (time(NULL));
int answer;
for(int i = 0; i < _nplayers; i++)
{
cout << _players[i].getName() << " answer the following questions:" << endl;

for(int j = 0; j < _nasked; j++)
{
int r = rand() % _nquestions;
_questions[r].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;
}




This post was edited by Minkomonster on May 5 2014 10:48pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 5 2014 11:28pm
Misreaded the question. This cycles through the players correctly

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(){ }
void display();
static int getNumberOfChoices();
bool judge(int);
string getText();
int getAnswer();
void setText(string);
void setAnswer(int);
void setChoices(string,string,string,string);
private:
static const int _nchoices = 4;
string _text;
string _choice1,_choice2,_choice3,_choice4;
int _answer;

};

int Question::getNumberOfChoices()
{
return _nchoices;
}

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;
const int nchoices = Question::getNumberOfChoices();
string text;
string choices[nchoices];


for(int i = 0; i < _nquestions; i++)
{
cout << "Enter a question:";
cin >> text;
for(int j = 0; j < nchoices; j++)
{
cout << "Enter choice " << (j+1) << " of " << 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;
}


This post was edited by Minkomonster on May 5 2014 11:43pm
Member
Posts: 1,995
Joined: Jun 28 2006
Gold: 7.41
May 6 2014 04:23pm
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;
}
Member
Posts: 9,803
Joined: Jun 28 2005
Gold: 6.67
May 7 2014 12:19am
I understand this is a homework question, but I don't think that teaching him wrong habits is a good idea.
Member
Posts: 9,624
Joined: Sep 7 2008
Gold: 3,272.00
May 7 2014 12:31am
Quote (KrzaQ2 @ May 7 2014 01:19am)
I understand this is a homework question, but I don't think that teaching him wrong habits is a good idea.


If you are referring to me using this method to get my homework done, I am incapable of coding or typing in length at the moment.
Regardless, trade is done, close this please

This post was edited by matthan on May 7 2014 12:54am
Go Back To Programming & Development Topic List
12Next
Add Reply New Topic New Poll