-
Notifications
You must be signed in to change notification settings - Fork 0
/
SimpleMathsQuiz.c
305 lines (280 loc) · 15.4 KB
/
SimpleMathsQuiz.c
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
// COPYRIGHT Nicola Mahon C15755031
/* Program to create a simple maths quiz, including options
for the user to control the game via a menu.
User can select 4 options from the Main Menu:
1: to select the number of questions they would like to answer per round.
2: to start the quiz.
3: to display their score for the round.
4: to exit or quit the quiz.
Rules/Controls:
- There are a maximum of 5 questions per round.
- The user cannot start the quiz (2) without having first selected the number of questions to be asked (1).
- The user cannot display their score (3) without having first selected the number of questions to be asked (1) AND without having played at least one round (2).
- Scores are reset after each round and do not accumulate.
- Each round is numbered.
- There is no limit on the number of rounds that can be played.
Name: Nicola Mahon C15755031
Date: 2015-10-30
*/
// include standard input/output library
#include <stdio.h>
// set max and min sizes for answer array, num_questions
#define MAX_SIZE 5
#define MIN_SIZE 1
main()
{
// array of answers to the maths questions
int answers[MAX_SIZE] = {88, 228, 156, -4, 1};
// variable to store the users selected menu option
int option;
// variables to store correct /incorrect answer scores
int score = 0;
int wrong = 0;
// variable to hold the users selection in Menu Option 1 (number of questions to be asked)
int num_questions = 0;
// index variable for looping through array and also controlling number of questions asked each round
int i;
// variable to store users answers to questions
int user_answer;
// to count the rounds played by the user (if replaying)
int round = 1;
// welcome message to user
printf("\t\t\t*** FUN MATHS QUIZ ***\t\t\t\n");
printf("\n\t\t\tWelcome to your Maths Quiz!\t\t\t");
printf("\n\tPlease select one of the following options from the Main Menu below...\t\n");
// do_while: to print the main menu once, and loop until the user selects '4' i.e. to exit the game
do
{
// print the main menu once, and loop back after each menu option ends
printf("\n\t\t\t*** MAIN MENU ***\t\t\t\n");
printf(">>> Select 1: to select the number of QUESTIONS you would like to answer.\n");
printf(">>> Select 2: to START the quiz.\n");
printf(">>> Select 3: to display your SCORE for the ROUND.\n");
printf(">>> Select 4: to EXIT the quiz.\n");
// ask user to select a menu option, store in scanf (option)
printf("\n>>> Please select a menu option: ");
scanf("%d", &option);
flushall();
// check for user selecting invalid menu option
if (option != 1 && option != 2 && option != 3 && option != 4)
{
printf("\nThis is not a valid option.\n");
}
else if (option == 4)
{
printf("\nGoodbye");
printf("\nPress 'enter' to exit.");
}
// switch statement to control the menu options (1-3), 4 exits the program and is not in the switch statement
switch(option)
{
// Menu Option 1: user chooses how many questions to answer in this round
case 1:
{
// print this once in do_while and repeat until program receives a valid selection (1-5)
do
{
// print instructions to user, store in scanf (num_questions)
printf("\n--- You selected Menu Option: %d", option);
printf("\nThere are a maximum of %d questions per round.\n", MAX_SIZE);
printf("Please enter number of questions for this round: ");
scanf("%d", &num_questions);
// this variable (num_questions) will be reset at the end of each round so that the user
// has to select the number of questions again, if they are playing another round
flushall();
// output to user their chosen selection (1-5)
printf("--- You have selected to answer %d questions this round.\n", num_questions);
// check for user selecting invalid option i.e. less than 1 or greater than 5
if (num_questions > MAX_SIZE || num_questions < MIN_SIZE)
{
printf("\n*** ERROR: Please select a number up to max of %d!\n", MAX_SIZE);
}
} // repeat looping in do_while until user selects number between 1-5
while (num_questions > MAX_SIZE || num_questions < MIN_SIZE);
break;
} // end case 1, how many questions to answer in this round
// Menu Option 2: start the quiz and print the questions
case 2:
{
// control to prevent the user beginning the quiz without having first selected menu option 1 (num of questions)
if (num_questions == 0)
{
printf("\n*** ERROR: Please select the number of questions first!\n");
break;
}
// if user has selected number of questions, begin printing questions
else
{
// set all score and index counters to zero (used repeatedly when playing multiple rounds)
score = 0;
wrong = 0;
i = 0;
// output to user, their chosen selection, and round X of quiz is now starting
printf("\n--- You selected Menu Option: %d", option);
printf("\n--- Let's begin...\n");
printf("\n\t***** ROUND %d *****\n", round);
// do_while; prints the first question when quiz starts (DO)
// and proceeds to next question as long as [i] index is less than the num_questions (WHILE)
do
{
// Question 1
// print question, store in scanf (user_answer)
printf("\nQuestion 1: >> (9 x 8) + 16 = _ \n");
scanf("%d", &user_answer);
flushall();
// check if answer is same as [i] index in answers array (initialized with values at start)
if (user_answer == answers[i])
{
// if correct, print correct, increment score counter
printf("--- You entered: %d - Correct!!\n", user_answer);
score++;
}
else
{
// if wrong, print wrong, increment wrong counter
printf("--- You entered: %d - Wrong!! The correct answer: %d\n", user_answer, answers[i]);
wrong++;
}
// increment [i] index so that if the do_while evaulates to false at next question (below),
//the program will exit out of the loop before asking the next question
i++;
// as explained (above), check if [i] is less than num_questions
// if false, exit loop otherwise move on and ask the next question
if (i < num_questions)
{
// Question 2
// print question, store in scanf (user_answer)
printf("\nQuestion 2: >> (121 - 83) x 6 = _ \n");
scanf("%d", &user_answer);
flushall();
// check if answer is same as [i] index in answers array (initialized with values at start)
if (user_answer == answers[i])
{
// if correct, print correct, increment score counter
printf("--- You entered: %d - Correct!!\n", user_answer);
score++;
}
else
{
// if wrong, print wrong, increment wrong counter
printf("--- You entered: %d - Wrong!! The correct answer: %d\n", user_answer, answers[i]);
wrong++;
}
// increment [i] index so that if the do_while evaluates to false,
//the program will exit out of the loop before the next question
i++;
} // end if Q2
// check if [i] is less than num_questions, if false, exit loop otherwise move on and ask the next question
if (i < num_questions)
{
// Question 3
// print question, store in scanf (user_answer)
printf("\nQuestion 3: >> 12 x (143 / 11) = _ \n");
scanf("%d", &user_answer);
flushall();
// check if answer is same as [i] index in answers array (initialized with values at start)
if (user_answer == answers[i])
{
// if correct, print correct, increment score counter
printf("--- You entered: %d - Correct!!\n", user_answer);
score++;
}
else
{
// if wrong, print wrong, increment wrong counter
printf("--- You entered: %d - Wrong!! The correct answer: %d\n", user_answer, answers[i]);
wrong++;
}
// increment [i] index so that if the do_while evaluates to false,
//the program will exit out of the loop before the next question
i++;
} // end if Q3
// check if [i] is less than num_questions, if false, exit loop otherwise move on and ask the next question
if (i < num_questions)
{
// Question 4
// print question, store in scanf (user_answer)
printf("\nQuestion 4: >> (56 x 3) - 172 = _ \n");
scanf("%d", &user_answer);
flushall();
// check if answer is same as [i] index in answers array (initialized with values at start)
if (user_answer == answers[i])
{
// if correct, print correct, increment score counter
printf("--- You entered: %d - Correct!!\n", user_answer);
score++;
}
else
{
// if wrong, print wrong, increment wrong counter
printf("--- You entered: %d - Wrong!! The correct answer: %d\n", user_answer, answers[i]);
wrong++;
}
// increment [i] index so that if the do_while evaluates to false,
//the program will exit out of the loop before the next question
i++;
} // end if Q4
// check if [i] is less than num_questions, if false, exit loop otherwise move on and ask the next question
if (i < num_questions)
{
// Question 5
// print question, store in scanf (user_answer)
printf("\nQuestion 5: >> 216 / (18 x 12) = _ \n");
scanf("%d", &user_answer);
flushall();
// check if answer is same as [i] index in answers array (initialized with values at start)
if (user_answer == answers[i])
{
// if correct, print correct, increment score counter
printf("--- You entered: %d - Correct!!\n", user_answer);
score++;
}
else
{
// if wrong, print wrong, increment wrong counter
printf("--- You entered: %d - Wrong!! The correct answer: %d\n", user_answer, answers[i]);
wrong++;
}
} // end if Q5
} // end DO (menu option 2)
while (i < num_questions-1);
// display notice of End of Round to user
printf("\n>>> That is the end of Round %d.\n", round);
// increment round counter for next round
round++;
break;
} // end ELSE, printing quiz questions
} // end case 2, start the quiz and print the questions
// Menu Option 3: Print the player's scores for the round
case 3:
{
// control to prevent the user choosing menu option 3 (print score)
// without first having selected num of questions and playing at least one round
if (num_questions == 0)
{
printf("\n*** ERROR: You must play the game first!\n");
//break;
}
// at least one round must have been played
else if (score == 0 && wrong == 0)
{
printf("\n*** ERROR: You must play the game first!\n");
//break;
} // end inner IF
else
{
// if above conditions are false, print the scores; correct and wrong
printf("\nScore for Round %d: %d CORRECT, %d WRONG.\n", round-1, score, wrong);
// ask user if they want to play again
printf("\n>>> Would you like to play again? Press 1 (yes) / 4 (EXIT)\n");
// reset the num_questions counter for new round so that loop will start again
num_questions = 0;
break;
} // end else
} // end case 3, printing scores for the round
} // end switch (option)
} // end DO; printing the main menu once, and looping back after each menu option ends, until user quits (i.e until option = 4)
while (option != 4);
// to keep the window open for the user
getchar();
} // end main()