Quiz model
- C course
- Intermediate
- Lesson 42/116
- 6 min
Design the skeleton of Question and Quiz structs.
Project: Quiz app
In this five-lesson project you'll build a quiz app. Each lesson adds a piece; by the final lesson you'll have a program that combines all parts.
Throughout the project you'll define a struct Question type, add questions, run the quiz, and compute score and percentage. The functions you'll define: add_question, run_quiz, print_percentage and run_quiz_app. In this first lesson you'll design the model: the struct Question definition and the main skeleton.
Question struct
struct Question models a question. It has two core fields: text holds the question text, correct holds the index of the correct answer.
Example: struct Question { char text[64]; int correct; };. Here text is a 64-byte string, correct is an integer 0-3. This design represents multiple-choice questions simply; options are kept separate. You'll use this struct throughout the project; in the next lessons you'll add and play questions.
Practice Write the quiz skeleton
Define struct Question with char text[64]; and int correct;. Write int main(void) and inside it print "Quiz hazır" with printf("Quiz hazır\n");.
Starter code
#include <stdio.h>
// Define struct Question (text[64] and correct)
// Write main, print "Quiz hazır"Sample solution
#include <stdio.h>
struct Question {
char text[64];
int correct;
};
int main(void) {
printf("Quiz hazır\n");
return 0;
}Expected output
Quiz hazır
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What does struct Question store?
- Only the question text
- Only the answer
- The question count
- Question text and correct answer Correct answer
Explanation: struct Question stores both the question text and the correct answer.
2 Match fields to purpose
| Term | Meaning |
|---|---|
text |
Question text |
correct |
Correct answer |
struct Question |
Question type |
char text[64] |
Text field |
3 Complete the Question text field
struct Question { char ____[64]; int correct; };
struct Question { char text[64]; int correct; };
Explanation: text stores the question text.