-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathquiz.c
More file actions
135 lines (123 loc) · 4.1 KB
/
Copy pathquiz.c
File metadata and controls
135 lines (123 loc) · 4.1 KB
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
#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include<string.h>
void mainmenu();
void quizgame();
int main() {
mainmenu();
return 0;
}
void mainmenu() {
int ch;
FILE *high_score;
// Temporary until no 1 is created
high_score = fopen("high_score.txt", "w");
if (high_score == NULL) {
perror("Error opening file");
return;
}
fprintf(high_score, "prabin 20");
fclose(high_score);
printf("PRESS 1 To start the game.\n");
printf("PRESS 2 To check the high score.\n");
printf("PRESS 3 To update the question database.\n");
printf("PRESS 4 To exit the game (ps. by exiting the game you lose your chance to become smart)\n");
while (1) {
printf("\nENTER YOUR CHOICE: ");
scanf("%d", &ch);
while (getchar() != '\n'); // Clear the newline character left by scanf
switch (ch) {
case 1:
quizgame();
printf("............welcome to the game...............\n");
printf("the game is under construction\n\n");
printf("Press enter to go to main menu\n\n");
getch();
mainmenu();
return;
case 2:
high_score = fopen("high_score.txt", "r");
if (high_score == NULL) {
printf("file error\n\n");
printf("Press enter to go to main menu\n\n");
getch();
mainmenu();
return;
}
else {
char score[100];
fgets(score, sizeof(score), high_score);
printf("%s\n\n", score);
fclose(high_score);
printf("Press enter to go to main menu\n\n");
getch();
mainmenu();
return;
}
case 3:
{
FILE *f;
char question[200];
f = fopen("questionbase.txt", "a");
if (f == NULL) {
perror("Error opening file");
printf("Press enter to go to the main menu\n\n");
getch();
mainmenu();
return;
} else {
printf("Enter the question that you want to store\n");
// getchar();
// scanf("%[^\n]", question);
fgets(question,200,stdin);
fprintf(f, "%s", question);
fclose(f);
printf("String stored in file successfully!\n");
printf("enter to go back to the main menu\n\n");
getch();
mainmenu();
return;
}
}
case 4: {
char oi;
printf("Are you sure you want to exit (y for yes, any other key for no)?\n");
scanf(" %c", &oi);
while (getchar() != '\n'); // Clear the newline character left by scanf
if (oi == 'y' || oi == 'Y') {
exit(0);
} else {
mainmenu();
return;
}
}
default:
printf("Please choose among 1, 2, 3, 4\n\n");
printf("Press enter to go to main menu\n\n");
getch();
mainmenu();
return;
}
}
}
void quizgame()
{
int cg;
printf("1.General Konwledge\n");
printf("2.Computer Science\n");
printf("Please enter among any of this choices: ");
scanf("%d", &cg);
switch (cg)
{
case 1:
printf("General Knowledge\n");
break;
case 2:
printf("Computer Science\n");
break;
default:
printf("Invalid Choice");
break;
}
}