-
Notifications
You must be signed in to change notification settings - Fork 0
/
QuizLite.c
97 lines (88 loc) · 2.92 KB
/
QuizLite.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int marks = 0, right = 0, wrong = 0; //global static variables
//function prototypes
void load(int lineNumber, char *lineData);
void parseOption(char *line, int *ans, int an);
void checkAns(int *input, int* ans);
//main function
int main(){
printf ("**Welcome to QuizLite!**\nDo you want to start quiz? [Y/n]: ");
char line[256];
int q, a, qn, an, ans;
int input;
qn = 0;
q = 1;
while (getch()!= 'n' && q<27){
system("cls"); //clear screen after every question
printf ("QuizLite\n--------\n");
load(q, line); //load question to character array line
printf("\n[%d] %s\n",++qn, line); //print character array line
an = 0;
//load options
for (a=q+1;a<q+5;a++){
memset (line, '\0', 256); //to clear character array line
load(a, line); //load options to character array line
parseOption(line, &ans, ++an); //parse an option to check if right or wrong and print
}
printf("\n> ");
scanf("%d", &input); //input answer
checkAns(&input, &ans); //check answer
q+=5;
printf ("\nProceed to Next Question? [Y/n]: \n");
}
system("cls"); //clear screen before printing score
printf ("QuizLite\n--------\n");
printf ("\n\nYour score is: %d\n", marks);
printf ("[%d Correct, %d Wrong]", right, wrong);
return 0;
}
//stores specific line data to a character pointer
void load(int lineNumber, char *lineData){
static const char filename[] = "qa_data.data"; //location of file containing formatted questions and options
FILE *file = fopen(filename, "r"); //open file in read-only mode
int count = 1;
if ( file != NULL ) {
char line[256];
while (fgets(line, sizeof line, file) != NULL) {
if (count == lineNumber) {
strcpy(lineData, line); //copies specific line to character pointer lineData
fclose(file);
}
else {
count++;
}
}
fclose(file);
}
else {
strcpy(lineData, "Invalid File Request"); //if file is not found
}
}
//checks if an option is right answer or not and print the options
void parseOption(char *line, int *ans, int an){
int i;
//search for '*' character before right answer
if (line[0] == '*'){
*ans = an; //store the correct option number
}
printf ("(%d) ", an); //print option number
for (i=1;i<80;i++){
putchar (line[i]); //print option
}
printf("\n");
}
//checks if inputted answer is right or wrong.
void checkAns(int *input, int* ans){
if (*input == *ans){
marks+=10;
right++;
printf ("[Correct Answer]\n");
}
else {
wrong++;
printf("[Wrong Answer]\n");
printf("Correct Answer is: %d\n", *ans);
}
}