Register Now

Login

Lost Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Login

Register Now

Welcome to All Test Answers

Trivia game for two players using C++

Trivia Game
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 question.
Make up your own trivia questions on the subject or subjects of your choice for
the objects.

Answer:


//  Trivia Game
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

// Question Class
class Question
{
    // Member variables
    private:
        // The trivia question
        string questionText;

        // An array to hold 4 possible answers.
        string possibleAnswers[4];

        // The number (1,2,3, or 4) of the
        // correct answer.
        int correctAnswer;
    
    // Member functions
    public:
        // Default constructor
        // (We will use the mutators to set the values of
        //  the member variables.)
        Question()
        { }

        // Accessors
        string getQuestion()
        {
            return questionText;
        }

        string getPossAnswer1()
        {
            return possibleAnswers[0];
        }

        string getPossAnswer2()
        {
            return possibleAnswers[1];
        }

        string getPossAnswer3()
        {
            return possibleAnswers[2];
        }

        string getPossAnswer4()
        {
            return possibleAnswers[3];
        }

        int getCorrectNumber()
        {
            return correctAnswer;
        }

        string getCorrectAnswer()
        {
            return possibleAnswers[correctAnswer-1];
        }

        // Mutators
        void setQuestionText(string q)
        {
            questionText = q;
        }

        void setPossAnswer1(string ans)
        {
            possibleAnswers[0] = ans;
        }

        void setPossAnswer2(string ans)
        {
            possibleAnswers[1] = ans;
        }

        void setPossAnswer3(string ans)
        {
            possibleAnswers[2] = ans;
        }

        void setPossAnswer4(string ans)
        {
            possibleAnswers[3] = ans;
        }

        void setCorrect(int correctNum)
        {
            correctAnswer = correctNum;
        }
};

// Function prototypes
void initQuestions(Question [], int);
void displayQuestion(Question);

int main()
{
    const int NUM_QUESTIONS = 10; // Number of questions
    int playerOnePoints = 0;      // Player 1's points
    int playerTwoPoints = 0;      // Player 2's points

    // Array of Question objects
    Question questions[NUM_QUESTIONS];

    // Initialize the array with data from the trivia.txt file.
    initQuestions(questions, NUM_QUESTIONS);

    // Play the game.
    int questionNum = 0;
    while (questionNum < 10)
    {
        int answer;   // To hold the player's answer

        // Player 1's turn.
        cout << "Question for PLAYER 1:\n";
        cout << "----------------------\n";
        displayQuestion(questions[questionNum]);
        cout << "Enter the correct answer: "; cin >> answer;

        // Score the answer.
        if (answer == questions[questionNum].getCorrectNumber())
        {
            cout << "Correct!\n\n";
            playerOnePoints++;
        }
        else
        {
            cout << "Sorry, that's incorrect. The correct answer is "
                 << questions[questionNum].getCorrectNumber() << ". "
                 << questions[questionNum].getCorrectAnswer()
                 << endl << endl;
        }

        // Increment the question number for player 2.
        questionNum++;

        // Player 2's turn.
        cout << "Question for PLAYER 2:\n";
        cout << "----------------------\n";
        displayQuestion(questions[questionNum]);
        cout << "Enter the correct answer: "; cin >> answer;

        // Score the answer.
        if (answer == questions[questionNum].getCorrectNumber())
        {
            cout << "Correct!\n\n";
            playerTwoPoints++;
        }
        else
        {
            cout << "Sorry, that's incorrect. The correct answer is "
                 << questions[questionNum].getCorrectNumber() << ". "
                 << questions[questionNum].getCorrectAnswer()
                 << endl << endl;
        }

        // Increment the question number for the next round.
        questionNum++;
    }

    // Show each player's points.
    cout << "Game over. Here are the points:\n";
    cout << "PLAYER 1: " << playerOnePoints << endl;
    cout << "PLAYER 2: " << playerTwoPoints << endl << endl; // Declare the winner. if (playerOnePoints > playerTwoPoints)
    {
        cout << "PLAYER 1 WINS!\n"; } else if (playerTwoPoints > playerOnePoints)
    {
        cout << "PLAEYR 2 WINS!\n";
    }
    else
    {
        cout << "It's a tie!\n";
    }

    return 0;
}

// The initQuestions function reads trivia question data
// from the file trivia.txt and stores it in the array
// of Question objects.

void initQuestions(Question quest[], int size)
{
   const int SIZE = 500;  // Size of input array
   char input[SIZE];      // To hold file input
   fstream inputFile;     // File stream object
   int index = 0;         // Array indexer

   // Open the file in input mode.
   inputFile.open("trivia.txt", ios::in);
   if (!inputFile)
   {
      cout << "ERROR: Cannot open trivia file.\n";
      exit(0);
   }

   // Read the file contents.
   inputFile.getline(input, SIZE); 
   while (!inputFile.eof() && index < size)
   {
        // Set the question text.
        quest[index].setQuestionText(input);

        // Read and store the first possible answer.
        inputFile.getline(input, SIZE);
        quest[index].setPossAnswer1(input);

        // Read and store the second possible answer.
        inputFile.getline(input, SIZE);
        quest[index].setPossAnswer2(input);

        // Read and store the third possible answer.
        inputFile.getline(input, SIZE);
        quest[index].setPossAnswer3(input);

        // Read and store the fourth possible answer.
        inputFile.getline(input, SIZE);
        quest[index].setPossAnswer4(input);

        // Read and store the number of the correct answer.
        inputFile.getline(input, SIZE);
        quest[index].setCorrect(atoi(input));

        // Increment index.
        index++;

        // Read the next question text.
        inputFile.getline(input, SIZE);
   }
}

// The displayQuestion function displays a trivia question.

void displayQuestion(Question quest)
{
    cout << quest.getQuestion() << endl;
    cout << "ANSWERS:\n";
    cout << "1. " << quest.getPossAnswer1() << endl;
    cout << "2. " << quest.getPossAnswer2() << endl;
    cout << "3. " << quest.getPossAnswer3() << endl;
    cout << "4. " << quest.getPossAnswer4() << endl;
}


 

Trivia.txt file


(1) What famous document begins: "When in the course of human events..."?
The Gettysburg Address
The US Declaration of Independence
The Magna Carta
The US Bill of Rights
2
(2) Who said "A billion dollars isn't worth what it used to be"?
J. Paul Getty
Bill Gates
Warren Buffet
Henry Ford
1
(3) What number does "giga" stand for?
One thousand
One million
One billion
One trillion
3
(4) What number is 1 followed by 100 zeros?
A quintillion
A google
A moogle
A septaquintillion
2
(5) Which of the planets is closest in size to our moon?
Mercury
Venus
Mars
Jupiter
1
(6) What do you call a group of geese on the ground?
skein
pack
huddle
gaggle
4
(7) What do you call a group of geese in the air?
skein
pack
huddle
gaggle
1
(8) Talk show host Jerry Springer was the mayor of this city.
Chicago
Indianapolis
Cincinnati
Houston
3
(9) On a standard telephone keypad, the letters T, U, and V are matched to what number?
5
6
7
8
4
(10) Crickets hear through this part of their bodies.
Head
Knees
Ears
Tail
2


About

Leave a reply

Captcha Click on image to update the captcha .

error: Content is protected !!