Keeping Score on a Quiz?

Hey everyone, I just made a post regarding a project I'm working on and I have another question. I'm writing a program that has the user select from 3 different quizzes. So far, I have it so the user can select what quiz they want to take and actually take it. I want to be able to keep track of their score and tell them their percentage at the end of the quiz. Each quiz has 5 questions so each wrong answer would deduct 20% (Or each right answer would give them 20%. Either way works for me.). How would I keep track of their right/wrong answers and then tell them their percentage at the end of the quiz? Any advice is appreciated! (I had to leave out one of my functions so I didn't exceed the character limit.)

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
249
250
251
252
253
254
255
256
257
258
259
260
#include <iostream>
#include <string>
#include <fstream>
using namespace std;

void geographyQuiz();
void basicMathQuiz();
void randomTriviaQuiz();

int main()
{
	string name;
	cout << "Hello! Please enter your name: ";
	getline(cin, name);
	
	int quizSelection;
	cout << "Please make a selection. Enter 1, 2, or 3\n";
	cout << "1. Geography Quiz\n";
	cout << "2. Basic Math Quiz\n";
	cout << "3. Random Trivia Quiz\n";
	cin >> quizSelection;

	if (quizSelection == 1)
	{
		geographyQuiz();
	}
	else if (quizSelection == 2)
	{
		basicMathQuiz();
	}
	else if (quizSelection == 3)
	{
		randomTriviaQuiz();
	}

	system("pause");
	return 0;

}

void geographyQuiz()
{
	// Intro to test.
	cout << "Let's test your geography skills!\n";
	cout << "I will ask you five questions.\n";
	cout << "Press the number(s) on your keypad that corresponds with your answer,\n";
	cout << "then press 'Enter' to select an answer. Good Luck!\n";
	cout << "\n\n\n";
	cout << "Geography Test" << endl;
	cout << "--------------" << endl;

	// Question A.
	const string questionA = "A. What continent is Estonia in? \n\n";
	int answerA;

	cout << questionA;
	cout << "1) South America\n";
	cout << "2) Europe\n";
	cout << "3) Asia\n";
	cout << "4) North America\n\n";
	cin >> answerA;
	if (answerA != 2)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}
		
	// Question B.
	const string messageB = "B. What is the capital of Canada?\n\n";
	int answerB;

	cout << messageB;
	cout << "1) Vancouver, BC\n";
	cout << "2) Calgary, AB\n";
	cout << "3) Ottawa, ON\n";
	cout << "4) Toronto, ON\n\n";
	cin >> answerB;
	if (answerB != 3)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}
		
	// Question C.
	const string messageC = "C. In what country is Cape Town?\n\n";
	int answerC;

	cout << messageC;
	cout << "1) South Africa\n";
	cout << "2) China\n";
	cout << "3) Hungary\n";
	cout << "4) Egypt\n\n";
	cin >> answerC;
	if (answerC != 1)
		{
			cout << "That is incorrect! 20% has been deducted from your score.\n\n";
		}
	else
	{
		cout << "That is correct!\n\n";
	}
				
	// Question D. Two are correct. Only select one correct answer.
	const string messageD = "D. Which of the following cities is in California? (Two are correct)\n\n";
	int answerD;

	cout << messageD;
	cout << "1) Fresno\n";
	cout << "2) Indianapolis\n";
	cout << "3) Oakland\n";
	cout << "4) Chicago\n\n";
	cin >> answerD;
	if (answerD != 1 && answerD != 3)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}
					
	// Question E. Two are correct. Select both correct answers.
	const string messageE = "E. Russia is in two continents. Which two? (Select more than one letter)\n\n";
	int answerE1;
	int answerE2;

	cout << messageE;
	cout << "1) North America\n";
	cout << "2) Antarctica\n";
	cout << "3) Asia\n";
	cout << "4) Europe\n\n";
	cin >> answerE1;
	cin >> answerE2;
	if (answerE1 == 3 || answerE1 == 4 && answerE2 == 3 || answerE2 == 4)
	{
		cout << "That is correct!\n\n";
	}
	else
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
				
	system("pause");
}

void basicMathQuiz()
{
	cout << "Welcome! Let's test your basic math skills!\n\n";
	cout << "I will ask you five questions.\n";
	cout << "Press the number(s) on your keypad that corresponds with your answer,\n";
	cout << "then press 'Enter' to select an answer. Good Luck!\n";
	cout << "\n\n\n";
	cout << "Basic Math Quiz" << endl;
	cout << "---------------" << endl;

	// Question A
	const string mathMessageA = "A. What is 2+20?\n\n";
	int mathAnswerA;

	cout << mathMessageA << endl;
	cout << "1. 22\n";
	cout << "2. 48\n";
	cout << "3. 77\n";
	cout << "4. 15\n";
	cin >> mathAnswerA;
	if (mathAnswerA != 1)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}

	// Question B. 
	const string mathMessageB = "B. What is 2+2?\n\n";
	int mathAnswerB;

	cout << mathMessageB << endl;
	cout << "1. 22\n";
	cout << "2. 48\n";
	cout << "3. 4\n";
	cout << "4. 15\n";
	cin >> mathAnswerB;
	if (mathAnswerB != 3)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}

	// Question C.
	const string mathMessageC = "What is 5-5?\n\n";
	int mathAnswerC;

	cout << mathMessageC << endl;
	cout << "1. 22\n";
	cout << "2. 48\n";
	cout << "3. 4\n";
	cout << "4. 0\n";
	cin >> mathAnswerC;
	if (mathAnswerC != 4)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}

	// Question D
	const string mathMessageD = "Pick an equation that equals 0. Two could be right.\n\n";
	int mathAnswerD;

	cout << mathMessageD << endl;
	cout << "1. 0x0\n";
	cout << "2. 1+8\n";
	cout << "3. 75-75\n";
	cout << "4. 100+48\n";
	cin >> mathAnswerD;
	if (mathAnswerD != 1 && mathAnswerD != 3)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}

	// Question E.
	const string mathMessageE = "Pick the two equations that equal 20.\n\n";
	int mathAnswerE1;
	int mathAnswerE2;

	cout << mathMessageE << endl;
	cout << "1. 5x4\n";
	cout << "2. 1+8\n";
	cout << "3. 20x1\n";
	cout << "4. 100+48\n";
	cin >> mathAnswerE1;
	cin >> mathAnswerE2;
	if (mathAnswerE1 == 1 || mathAnswerE1 == 3 && mathAnswerE2 == 1 || mathAnswerE2 == 3)
	{
		cout << "That is incorrect! 20% has been deducted from your score.\n\n";
	}
	else
	{
		cout << "That is correct!\n\n";
	}

	system("pause");
}
Last edited on
I am pretty new to programming, but might I suggest a global variable that you can use in each of your sub functions. You could utilize it within the if statement where the answers are incorrect. It could output at the end of your main. I am also not an expert in any way when it comes to this, but most posts I have read on here recommend staying away from using the system("pause").
Last edited on
Hey, thanks for your advice. I ended up creating variables for each question and assigning those variables to numbers based on if the question was answered right or wrong. Then, at the end of the quiz, I add the variables together and output the user's percentage. It works for the purposes of my project, although it may not be the best way to do it. And yeah, the system("pause") isn't the best but it works for the purposes of my project. I'm still a beginner but I've heard that once you get more experienced and advanced with C++ the system("pause") becomes very unnecessary. Again, thanks for your advice!
Topic archived. No new replies allowed.