-
Notifications
You must be signed in to change notification settings - Fork 0
/
Quiz.c
184 lines (151 loc) · 5.18 KB
/
Quiz.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
//includes extra resources the program uses
#include <stdio.h>
#include<stdlib.h>
#include <string.h>
#include "question.h" //include the question structure
#include "player.h" //include the player structure
void run_question_loop();//declare the run loop function
//amount of question in the array
int questions_amount = 10;
Question questions[10]; //create an array of 10 questions
Player player;
char quit = 'q'; //in case user wants to exit the program
int main()
{
init();
//intro to the quiz
printf("Amount of Questions: %d \n", questions_amount);
printf("Type \"%c\" to quit\n\n", quit); //tell the user that they may quit by typing 'q'
printf("Lives: %i\n", player.lives); //print the amount of lives they have
set_questions(); //set up all of the questions
run_question_loop(); //run the main loop that will ask and re-ask the questions
}
void init()
{
printf("Please Enter Your Name: ");
gets(player.name);
player.lives = 3;
}
//the main quiz loop
void run_question_loop()
{
int i; //interator
//loops through each question and evaluates the answer
for (i = 0; i < questions_amount; i++)
{
char answer[10]; /*stores answer of user*/
//asks the question by printing it on the screen
print_question(questions[i]);
//prints choices for user
print_answers(questions[i]);
//prints "Answer"
printf("Answer: ");
//allows user to asnwer the question, and stores the answer in the answer variable
gets(answer);
//stores the answer correctness result
int answer_result = check_answer(answer[0], questions[i].answer);
//checks the answer
if (!answer_result)
{
//checks of the user inputted 'q' to quit
check_quit(answer[0]);
//make the question repeat
i--;
//loose a life
lose_life(player);
}
//if this is the last question, then print "Great Job!" and the amount of lives
if (i == questions_amount - 1)
{
printf("Great Job :)\n");
printf("You ended with: %i lives!", player.lives);
}
}
}
//sets the questions up
void set_questions()
{
questions[0].number = 1;
strcpy(questions[0].question, "What is the code to comment in c?");
questions[0].answer = 'c';
questions[0].answers[0] = "\\\\";
questions[0].answers[1] = "**";
questions[0].answers[2] = "/**/";
questions[0].answers[3] = "\\**\\";
questions[1].number = 2;
strcpy(questions[1].question, "What is the initializing function called?");
questions[1].answer = 'b';
questions[1].answers[0] = "init()";
questions[1].answers[1] = "main()";
questions[1].answers[2] = "start()";
questions[1].answers[3] = "initialize()";
questions[2].number = 3;
strcpy(questions[2].question, "What is the code for printing to the console?");
questions[2].answer = 'a';
questions[2].answers[0] = "printf();";
questions[2].answers[1] = "prinf();";
questions[2].answers[2] = "print();";
questions[2].answers[3] = "type();";
questions[3].number = 4;
strcpy(questions[3].question, "How do you store a string array in c?");
questions[3].answer = 'd';
questions[3].answers[0] = "string[] arrName";
questions[3].answers[1] = "char[] arrName";
questions[3].answers[2] = "string arrName";
questions[3].answers[3] = "*char[] arrName";
questions[4].number = 5;
strcpy(questions[4].question, "What is the proper syntax for an if statement in c?");
questions[4].answer = 'c';
questions[4].answers[0] = "if (condition) {body};";
questions[4].answers[1] = "if (body) {condition}";
questions[4].answers[2] = "if (condition) {body}";
questions[4].answers[3] = "if (\"true\") {body}";
questions[5].number = 6;
strcpy(questions[5].question, "What library is used for input and output?");
questions[5].answer = 'b';
questions[5].answers[0] = "#include <stdlib.h>";
questions[5].answers[1] = "#include <stdio.h>";
questions[5].answers[2] = "#include <time.h>";
questions[5].answers[3] = "#include <string.h>";
questions[6].number = 7;
strcpy(questions[6].question, "Each line in c must end in:");
questions[6].answer = 'd';
questions[6].answers[0] = ":";
questions[6].answers[1] = ".";
questions[6].answers[2] = "!";
questions[6].answers[3] = ";";
questions[7].number = 8;
strcpy(questions[7].question, "C is a:");
questions[7].answer = 'a';
questions[7].answers[0] = "Structured Language";
questions[7].answers[1] = "OOP Language";
questions[7].answers[2] = "Both";
questions[7].answers[3] = "None";
questions[8].number = 9;
strcpy(questions[8].question, "C is most similar to:");
questions[8].answer = 'b';
questions[8].answers[0] = "Python";
questions[8].answers[1] = "C++";
questions[8].answers[2] = "C#";
questions[8].answers[3] = "Java";
questions[9].number = 10;
strcpy(questions[9].question, "C is missing the data type of:");
questions[9].answer = 'c';
questions[9].answers[0] = "Integer";
questions[9].answers[1] = "Floating Point Number";
questions[9].answers[2] = "String";
questions[9].answers[3] = "Char";
}
void check_quit(char answer)
{
if (answer == quit)
{
quit_quiz();
}
}
void quit_quiz()
{
//exits the quiz if user types in 'q'
printf("You Lose! :'(");
exit(0);
}