Help with trivia game

I almost got the whole program done except for a few issues that I couldn't resolve, but I think I know which parts need to be changed and I just don't know what I should change it to.

First, here is the text file that I am reading from. Note that it also has the correct answer number under each question's choices.

trivia.txt
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
Player 1:
Question One?
1: one
2: two
3: three
4: four
1
Player 1:
Question Two?
1: two
2: one
3: three
4: four
2
Player 1:
Question Three?
1: three
2: two
3: one
4: four
3
Player 1:
Question Four?
1: four
2: two
3: three
4: one
4
Player 1:
Question Five?
1: five
2: two
3: three
4: four
1
Player 2:
Question Six?
1: one
2: two
3: three
4: four
4
Player 2:
Question Seven?
1: two
2: one
3: three
4: four
1
Player 2:
Question Eight?
1: three
2: two
3: one
4: four
3
Player 2:
Question Nine?
1: four
2: two
3: three
4: one
2
Player 2:
Question Ten?
1: five
2: two
3: three
4: four
4


Here is my header file that I am not supposed to change at all.

Question.h
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
#ifndef QUESTION_H
#define QUESTION_H
using namespace std;

#include <string>

class Question
{
private:
	string triviaQuestion;
	string answerOne;
	string answerTwo;
	string answerThree;
	string answerFour;
	int correctAnswer;

public:
	Question();
	Question(string question, string one, string two, string three,
		string four, int answer);

	void setTriviaQuestion(string question);
	void setAnswerOne(string one);
	void setAnswerTwo(string two);
	void setAnswerThree(string three);
	void setAnswerFour(string four);
	void setCorrectAnswer(int answer);

	string getTriviaQuestion();
	string getAnswerOne();
	string getAnswerTwo();
	string getAnswerThree();
	string getAnswerFour();
	int getCorrectAnswer();

	bool checkGivenAnswer(int chosenNumber);

};


#endif 


Here is the implementation file.

Question.cpp
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
#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "Question.h"

using namespace std;

Question::Question()
{
	string triviaQuestion;
	string answerOne;
	string answerTwo;
	string answerThree;
	string answerFour;
	int correctAnswer;
}

Question::Question(string question, string one, string two, string three,
	string four, int answer)
{
	string triviaQuestion = question;
	string answerOne = one;
	string answerTwo = two;
	string answerThree = three;
	string answerFour = four;
	int correctAnswer = answer;
}

//mutators
void Question::setTriviaQuestion(string question)
{
	triviaQuestion = question;
}

void Question::setAnswerOne(string one)
{
	answerOne = one;
}

void Question::setAnswerTwo(string two)
{
	answerTwo = two;
}

void Question::setAnswerThree(string three)
{
	answerThree = three;
}

void Question::setAnswerFour(string four)
{
	answerFour = four;
}

void Question::setCorrectAnswer(int answer)
{
	correctAnswer = answer;
}

//accessors
string Question::getTriviaQuestion()
{
	return triviaQuestion;
}

string Question::getAnswerOne()
{
	return answerOne;
}

string Question::getAnswerTwo()
{
	return answerTwo;
}

string Question::getAnswerThree()
{
	return answerThree;
}

string Question::getAnswerFour()
{
	return answerFour;
}

int Question::getCorrectAnswer()
{
	return correctAnswer;
};

bool Question::checkGivenAnswer(int chosenNumber)
{
	int playerP = 0;
	cout << "Choose an answer by entering a number from 1 to 4." << endl;
	cin >> chosenNumber;
	cout << "\n" << endl;
	if (correctAnswer = chosenNumber)
	{
		playerP ++;
		return true;
	}
	else
	{
		return false;
	}
}


And finally, here is the main function file.

QuestionMain.cpp
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
#include <iostream>
#include <cassert>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
#include <ctime>
#include <cstdlib>
#include "Question.h"

using namespace std;

void questionAnswer(Question a[], int qNum)
{
	cout << a[qNum].getTriviaQuestion() << endl;
	cout << a[qNum].getAnswerOne() << endl;
	cout << a[qNum].getAnswerTwo() << endl;
	cout << a[qNum].getAnswerThree() << endl;
	cout << a[qNum].getAnswerFour() << endl;
}

int main()
{
	Question ques[10]; //Create an array of 10 Question objects
	int qNum = 0;
	int correctA = 0;
	string line;

	int player1A = 0; //Player 1 answers
	int player2A = 0; //Player 2 answers

	int player1P = 0; //Player 1 points
	int player2P = 0; //Player 2 points

	ifstream qFile;
	qFile.open("trivia.txt", ios::in);

	for (int lineN = 1; getline(qFile, line) && lineN <= 70; lineN++)

		if (lineN < 7)
		{
			cout << line << endl;
			ques[0].setTriviaQuestion(line);	//Question 1
			ques[0].setAnswerOne(line);
			ques[0].setAnswerTwo(line);
			ques[0].setAnswerThree(line);
			ques[0].setAnswerFour(line);
		}
		else if (lineN == 7)
		{
			ques[0].setCorrectAnswer(correctA);
			ques[0].checkGivenAnswer(player1A);
		}
		
		else if (lineN > 7 && lineN < 14)
		{
			cout << line << endl;
			ques[1].setTriviaQuestion(line);	//Question 2
			ques[1].setAnswerOne(line);
			ques[1].setAnswerTwo(line);
			ques[1].setAnswerThree(line);
			ques[1].setAnswerFour(line);
		}
		else if (lineN == 14)
		{
			ques[1].setCorrectAnswer(correctA);
			ques[1].checkGivenAnswer(player1A);
		}

		else if (lineN > 14 && lineN < 21)
		{
			cout << line << endl;
			ques[2].setTriviaQuestion(line);	//Question 3
			ques[2].setAnswerOne(line);
			ques[2].setAnswerTwo(line);
			ques[2].setAnswerThree(line);
			ques[2].setAnswerFour(line);
		}
		else if (lineN == 21)
		{
			ques[2].setCorrectAnswer(correctA);
			ques[2].checkGivenAnswer(player1A);
		}
		
		else if (lineN > 21 && lineN < 28)
		{
			cout << line << endl;
			ques[3].setTriviaQuestion(line);	//Question 4
			ques[3].setAnswerOne(line);
			ques[3].setAnswerTwo(line);
			ques[3].setAnswerThree(line);
			ques[3].setAnswerFour(line);
		}
		else if (lineN == 28)
		{
			ques[3].setCorrectAnswer(correctA);
			ques[3].checkGivenAnswer(player1A);
		}
		
		else if (lineN > 28 && lineN < 35)
		{
			cout << line << endl;
			ques[4].setTriviaQuestion(line);	//Question 5
			ques[4].setAnswerOne(line);
			ques[4].setAnswerTwo(line);
			ques[4].setAnswerThree(line);
			ques[4].setAnswerFour(line);
		}
		else if (lineN == 35)
		{
			ques[4].setCorrectAnswer(correctA);
			ques[4].checkGivenAnswer(player1A);
		}
		
		else if (lineN > 35 && lineN < 42)
		{
			cout << line << endl;
			ques[5].setTriviaQuestion(line);	//Question 6
			ques[5].setAnswerOne(line);
			ques[5].setAnswerTwo(line);
			ques[5].setAnswerThree(line);
			ques[5].setAnswerFour(line);
		}
		else if (lineN == 42)
		{
			ques[5].setCorrectAnswer(correctA);
			ques[5].checkGivenAnswer(player2A);
		}
		
		else if (lineN > 42 && lineN < 49)
		{
			cout << line << endl;
			ques[6].setTriviaQuestion(line);	//Question 7
			ques[6].setAnswerOne(line);
			ques[6].setAnswerTwo(line);
			ques[6].setAnswerThree(line);
			ques[6].setAnswerFour(line);
		}
		else if (lineN == 49)
		{
			ques[6].setCorrectAnswer(correctA);
			ques[6].checkGivenAnswer(player2A);
		}
		
		else if (lineN > 49 && lineN < 56)
		{
			cout << line << endl;
			ques[7].setTriviaQuestion(line);	//Question 8
			ques[7].setAnswerOne(line);
			ques[7].setAnswerTwo(line);
			ques[7].setAnswerThree(line);
			ques[7].setAnswerFour(line);
		}
		else if (lineN == 56)
		{
			ques[7].setCorrectAnswer(correctA);
			ques[7].checkGivenAnswer(player2A);
		}

		else if (lineN > 56 && lineN < 63)
		{
			cout << line << endl;
			ques[8].setTriviaQuestion(line);	//Question 9
			ques[8].setAnswerOne(line);
			ques[8].setAnswerTwo(line);
			ques[8].setAnswerThree(line);
			ques[8].setAnswerFour(line);
		}
		else if (lineN == 63)
		{
			ques[8].setCorrectAnswer(correctA);
			ques[8].checkGivenAnswer(player2A);
		}
		
		else if (lineN > 63 && lineN < 70)
		{
			cout << line << endl;
			ques[9].setTriviaQuestion(line);	//Question 10
			ques[9].setAnswerOne(line);
			ques[9].setAnswerTwo(line);
			ques[9].setAnswerThree(line);
			ques[9].setAnswerFour(line);
		}
		else if (lineN == 70)
		{
			ques[9].setCorrectAnswer(correctA);
			ques[9].checkGivenAnswer(player2A);
		}

	if (ques[qNum].getCorrectAnswer() == player1A)
	{
		player1P++;
	}
	else if (ques[qNum].getCorrectAnswer() == player2A)
	{
		player2P++;
	}

	cout << "Player 1 scored " << player1P << " points" << endl;
	cout << "Player 2 scored " << player2P << " points" << endl;

	if (player1P > player2P)
	{
		cout << "The winner is Player 1" << endl;
	}
	else if (player1P < player2P)
	{
		cout << "The winner is Player 2" << endl;
	}
	else
	{
		cout << "It's a tie!" << endl;
	}
}
The output of the program should look something like this sample:

Output
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
Player 1:
What is 1 + 1?
1: 1
2: 2
3: 3
4: 4
Choose an answer by entering a number from 1 to 4.
2

Player 1:
What is 2 + 2?
1: 1
2: 2
3: 3
4: 4
Choose an answer by entering a number from 1 to 4.
1

Player 1:
What is 3 + 3?
1: 2
2: 4
3: 6
4: 8
Choose an answer by entering a number from 1 to 4.
1

Player 1:
What is 4 + 4?
1: 2
2: 4
3: 6
4: 8
Choose an answer by entering a number from 1 to 4.
3

Player 1:
What is 5 + 5?
1: 7
2: 8
3: 9
4: 10
Choose an answer by entering a number from 1 to 4.
2

Player 2:
What is 6 + 6?
1: 12
2: 14
3: 16
4: 18
Choose an answer by entering a number from 1 to 4.
1

Player 2:
What is 7 + 7?
1: 12
2: 14
3: 16
4: 18
Choose an answer by entering a number from 1 to 4.
2

Player 2:
What is 8 + 8?
1: 12
2: 14
3: 16
4: 18
Choose an answer by entering a number from 1 to 4.
3

Player 2:
What is 9 + 9?
1: 12
2: 14
3: 16
4: 18
Choose an answer by entering a number from 1 to 4.
1

Player 2:
What is 10 + 10?
1: 16
2: 18
3: 20
4: 22
Choose an answer by entering a number from 1 to 4.
4

Player 1 scored 1 points
Player 2 scored 3 points
The winner is Player 2
Press any key to continue . . .


The good thing is that I am not getting any errors and the output looks good, but I am having two problems. My program is not counting the number of correct answers (points) for each player and I couldn't figure out how to read the lines that has the correct answer numbers from the text file.

I believe that first I need to fix this part from Question.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
bool Question::checkGivenAnswer(int chosenNumber)
{
	int playerP = 0;
	cout << "Choose an answer by entering a number from 1 to 4." << endl;
	cin >> chosenNumber;
	cout << "\n" << endl;
	if (correctAnswer = chosenNumber)
	{
		playerP ++;
		return true;
	}
	else
	{
		return false;
	}
}


I don't know how to fix this part. After I fix it, I also need help to make a couple changes to the main function file QuestionMain.cpp to read correct answers and points properly. If you think about fixing my program in a different method, that's fine, as long as it is good for beginners.

Any help will be greatly appreciated. If you find a stupid mistake, that's expected because I am just a beginner. At least I have done that much.
Line 7:
 
if (correctAnswer = chosenNumber)

You're using the assignment operator (=), not the comparison operator (==).

Lines 40-180: Your code is very fragile depending on the line numbers of the input file.
You really need to read the file in a more generic manner.

Oh I apologize for that mistake, thank you for telling me. but can you help me with the reading method? How do I read a specific line that has only one integer and put it into the integer variable to check for correct answer? I couldn't figure out how to read that line and store the integer value in a variable.
It is OK if my code is fragile and dependent on the line numbers of that specific file. As long as my program works for that file and gives me the correct output, I am OK with it. I will learn better generic ways next in advanced c++ levels. What I need now is how to get my program store the (1)correct answers, (2)each player's answers, and (3)each player's points. I don't think I need to do a lot of changes because I already have that in the program but it is just not doing it correctly. There are small things that I need to change and reorganize somehow to get it working. I hope someone can help me with that.
'It is OK if my code is fragile and dependent on the line numbers of that specific file."

It's not okay. We're not okay with it. Ugly code leads to bugs, problems, and headaches - plus, whenever you're asking for help you should treat others with respect and provide the best you can.

Secondly, whenever you see something like this:

1
2
3
4
string answerOne;
string answerTwo;
string answerThree;
string answerFour;


think twice. Why not just use:
string answers[4];

This even plays better with your "correct answer":

1
2
if(inputted_answer == answers[correct_answer])//or correct_answer-1, if we index from 1...
//do something. 


Code quality doesn't collide with knowledge. If you know that you write something bad, you can fix it, without waiting for some arbitrary timepoint.

Now that I look at your code, it's even more terrible; see this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
	if (lineN < 7)
		{
			cout << line << endl;
			ques[0].setTriviaQuestion(line);	//Question 1
			ques[0].setAnswerOne(line);
			ques[0].setAnswerTwo(line);
			ques[0].setAnswerThree(line);
			ques[0].setAnswerFour(line);
		}
		else if (lineN == 7)
		{
			ques[0].setCorrectAnswer(correctA);
			ques[0].checkGivenAnswer(player1A);
		}
		
		else if (lineN > 7 && lineN < 14)
		{
			cout << line << endl;
			ques[1].setTriviaQuestion(line);	//Question 2
			ques[1].setAnswerOne(line);
			ques[1].setAnswerTwo(line);
			ques[1].setAnswerThree(line);
			ques[1].setAnswerFour(line);
		}

I can already see the pattern. It's all over there. Whenever you copy-paste your own code, it's a bad sign. This one is a really bad code. You want to use a for loop. You could do something like this(pseudocode incoming):

1
2
3
4
5
6
int questionNum = 0;
while(NextChar != End Of File)
{
    SetQuestion();
    ++questionNum;
}

SetQuestion could be the body of first two ifs in your for loop. Look that it is far more concise, and it's obvious what's happening.

The better(clearer) code you write, the less bugs you make(because it's easier to find them). Please rewrite your code, so that numbers are in form of numbers, not words(questions[], not questionOne, questionTwo,... ; getQuestion(int), not getQuestionOne, getQuestionTwo,...).

This is called "refactoring" - you're rewriting the code to look better, or work better(or both). Please do so and update your code. Then it will be easier to help you.
Matthew, thank you so much for the help. I apologize if there was any misunderstanding and you thought that I wasn't being respectful.

I understand that this code can look a lot better and I will do my best to make it look better. However, note that as I said in my main post, I am not supposed to make any changes on the header file. It is given to me to implement directly. Unfortunately, I have to use stringOne, stringTwo,..etc. and questionOne, questionTwo..etc because these are given in the header file to use as it is. I will still try to do it using your advice and suggestions. Thank you!!
Last edited on
No hard feelings. I just criticize so that whoever ends up working with you doesn't have to work with ugly code. ;)

Anyway, I see that you still didn't rewrite parts of the code that you can. Please do so. It will help you learn how to program better, and result in easier code.

Back to the topic, there's something I don't like: you're asking the question immediately after loading it. What if one entry is malformed? You'd ask few questions, then crash. I'd prefer the app to crash immediately - generally you first load the file, then edit it/take actions on its data(unless the file is large, then you do load the parts and work on the parts, but that's different story and not the situation we have here).

Line 102 is wrong:
if (correctAnswer = chosenNumber)
as it was pointed out, but the whole function doesn't make much sense too. Why pass some variable, when it doesn't matter to you? chosenNumber could be declared local and the method would work the same.

To count the number of successfuly solved questions, I'd just create two ints in main: playerAAnswerCount, and playerBAnswerCount. Pass it to your checkGivenAnswer by reference(I guess that's what you had in mind), and then if the answer is correct - increment it.
You don't need to actually return anything from this method.

If you want to store something more, I'd suggest you create a simple class called Player, which would have a fields which would tell you how many points a player has, which questions he answered, and how. Points are just ints, answered questions could be pointers to the questions, copies of the questions, or just some strings with the question. Answers could be strings. All of these things(question and player's answer) could be stored in std::vector of std::pair of question and answer, or std::vector of answer.

Read about those and let me know if you don't understand anything.
Topic archived. No new replies allowed.