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:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
//  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
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
(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 !!