-
Notifications
You must be signed in to change notification settings - Fork 0
/
quizz.c
305 lines (254 loc) · 7.59 KB
/
quizz.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
#include <stdio.h>
#include <stdlib.h>
int i, coins=0, highscore=0;
char name[20], input, ans;
void main_menu();
void correct();
void incorrect();
void play();
void high_score();
void alter_high_score();
void ask_question(int value_of_question_in_coins, char question[200] , char a[100] , char b[100] , char c[100] , char d[100] , char correct_ans_small, char correct_ans_capital);
void main()
{
printf("Please type your name and press enter: ");
fflush(stdin);
gets(name);
system("cls");
printf("\nHello %s! \n ",name);
printf("\n...........WELCOME TO............\n");
printf("\n...........QUIZ GAME!............\n");
main_menu();
}
void main_menu()
{
char menu;
printf("\n\n MAIN MENU\n\n");
printf(" Key Function\n\n");
printf(" P Play\n");
printf(" R Rules\n");
printf(" C Credits\n");
printf(" H High score\n");
printf(" E Exit\n\n");
printf(" Please type an input and press enter-");
fflush(stdin);
scanf("%c",&input);
switch(input)
{
case 'P':
case 'p':
system("cls");
play();
break;
case 'R':
case 'r':
system("cls");
printf("RULES:\n\nThe rules of the game are simple.\n\n");
printf("The player is presented with a question having four options each.\n\n");
printf("Each question carries a particular number of coins.\n\n");
printf("The number of coins associated with each question is more than the number of coins carried by the previous one.\n\n");
printf("Correct answer rewards the player with the coins associated with the question.\n\n");
printf("An incorrect answer would lead to losing the game and the coins associated with the question.\n\n");
printf("Press M to go to the main menu.\n");
fflush(stdin);
scanf("%c",&menu);
if(menu=='m' || menu=='M')
{
system("cls");
main_menu();
}
else
{
system("cls");
printf("illegal input");
main_menu();
}
break;
case 'C':
case 'c':
system("cls");
printf("\nThis program was made by Kunal, Durgesh & Kshitij.\n\n");
printf("Press M to go to the main menu. ");
fflush(stdin);
scanf("%c",&menu);
if(menu=='m' || menu=='M')
{
system("cls");
main_menu();
}
else
{
system("cls");
printf("illegal input");
main_menu();
}
break;
case 'H':
case 'h':
system("cls");
high_score();
printf("\n\nPress M to go back to the main menu. ");
fflush(stdin);
scanf("%c",&menu);
if(menu=='m' || menu=='M')
{
system("cls");
main_menu();
}
else
{
system("cls");
printf("illegal input");
main_menu();
}
break;
case 'E':
case 'e':
printf("\nAre you sure you want to exit?[y-->exit/n-->back to main menu] ");
fflush(stdin);
scanf("%c",&menu);
if(menu== 'y' || menu=='Y')
{
system("cls");
printf("\nThank you for playing!");
exit(0);
}
else if(menu== 'N' || menu=='n')
{
system("cls");
main_menu();
}
else
{
system("cls");
printf("illegal input");
main_menu();
}
break;
default:system("cls");
printf("illegal input!\n\nPress E to exit\n\nPress any other key to go to main menu.");
fflush(stdin);
scanf("%s",&input);
if(input== 'E' || input== 'e')
{
system("cls");
exit(0);
}
else
{
system("cls");
main_menu();
}
break;
}
}
void correct()
{
printf("\n\nCorrect!\n\nScore=%d coins\n\nPress M to go to main menu.\n\nPress any other key to continue. ",coins);
fflush(stdin);
scanf("%c",&ans);
if(ans =='m' || ans=='M')
main_menu();
else
system("cls");
}
void incorrect()
{
system("cls");
printf("Your response is incorrect.\n");
alter_high_score();
}
void high_score()
{
FILE *fp1, *fp2;
char ch;
fp1=fopen("quiz_highscore.txt","r");
fp2=fopen("quiz_highscore_holder.txt","r");
if(fp1==NULL)
printf("\n\nCannot open target file. Contact the developer.");
printf("\nCurrent highscore is ");
while((ch=getc(fp1))!=EOF)
printf("%c",ch);
printf(" coins.\nThis amazing feat was archieved by ");
while((ch=getc(fp2))!=EOF)
printf("%c",ch);
printf(".");
fclose(fp1);
fclose(fp2);
}
void alter_high_score()
{
FILE *fp, *fp1, *fp2;
char highscore_as_char[10];
fp= fopen("quiz_highscore.txt", "r");
while(fscanf(fp, "%s", highscore_as_char)!=EOF)
{
//printf("%s ", buff );
}
highscore = atoi(highscore_as_char);
if(coins>highscore)
{
highscore=coins;
printf("\n\n************************************\nCongratulations %s!\nYou have made a new highscore of %d coins !\n************************************",name,coins);
fp1=fopen("quiz_highscore.txt","w");
fp2=fopen("quiz_highscore_holder.txt","w");
if(fp1==NULL || fp2 ==NULL)
printf("\n\nCannot open target file. Contact the developer.");
else
{
fprintf(fp1,"%d",coins);
fprintf(fp2,"%s",name);
}
fclose(fp1);
fclose(fp2);
}
else if(coins<highscore)
{
printf("\n\nYou have earned %d coins but that is not enough to beat the highscore...\nTry again!",coins);
}
else if(coins=highscore)
{
printf("\n\n*****************************************\nYou have earned %d coins %s.\nThat is the same as the current highscore!\n*****************************************",coins,name);
}
printf("\n\npress E to exit.\n\nPress any other key to go back to main menu.");
fflush(stdin);
scanf("%s",&input);
if(input== 'E' || input== 'e')
{
system("cls");
exit(0);
}
else
{
system("cls");
main_menu();
}
}
void ask_question(int value_of_question_in_coins,char question[100] , char a[50] , char b[50] , char c[50] , char d[50] , char correct_ans_small, char correct_ans_capital)
{
printf("\nFor %d coins\n\nQuestion: %s?\n\nA)%s\n\nB)%s\n\nC)%s\n\nD)%s\n\nType your response and press enter-", value_of_question_in_coins, question, a, b, c, d);
fflush(stdin);
scanf("%c",&ans);
if(ans == correct_ans_capital || ans == correct_ans_small)
{
coins= coins+ value_of_question_in_coins;
correct();
}
else
{
incorrect();
}
}
void play()
{
ask_question(10,"What is the capital of India?", "Islamabad", "Delhi" , "Mumbai", "Jaipur", 'b', 'B');
ask_question(20,"What is the capital of Nepal?", "Padampuri", "Newai", "Kathmandu", "Kumaribadi", 'c', 'C');
ask_question(40,"Who was the first Indian to go to space?", "Rakesh Verma", "Rakesh Sharma", "Rakesh Shrivastav", "Rakesh J. Sharma", 'b', 'B');
ask_question(100,"What hich of the following is not a fruit?", "Tomatoe", "Onion", "Apple", "Grape", 'b', 'B');
ask_question(250,"What was the surname of the famous physist Stephan?", "Harvins", "Hawkings", "Hawkins", "Hawking", 'd', 'D');
ask_question(600,"What is India's highest civillian honour?", "Paramveer chakra", "Bharat Ratna", "Mahaveer Ratna", "Tejaswi Chakra", 'b', 'B');
ask_question(1000,"Who is also called the \"Missile man\" of India?", "Abdul kalam Azad", "A.P.J. Abdul Kalam Azad", "A.P.J Abdul Kalam", "Abdul Kalam", 'c', 'C');
ask_question(2500,"What is the real colour of sunlight?", "White", "Blue", "Yellow", "Colourless", 'a', 'A');
ask_question(5000,"What was Mahatma Gandhi\'s middle name?", "Karamvir", "Karamdas", "Karamchandra", "Karamkhot", 'c', 'C');
ask_question(7500,"Why does most part of earth look blue from the space?", "Because clouds and sky are blue", "Because of god\'s will", "No one knows", "Because a large part of earth is covered in water", 'd', 'D');
}