C
Putting it together
Combine the quiz functions with a console menu.
Putting it together
The final lesson defines the run_quiz_app function. This function gathers the whole quiz flow in one place: greeting, question adding, running, percentage and a farewell message.
Signature: void run_quiz_app(void). Body: starts with printf("Quiz hazır\n");; declares struct Question questions[10]; int count = 0;; adds 2 questions with add_question; prints printf("%d soru eklendi\n", count);; calls run_quiz and print_percentage; finally prints printf("Tamamlandı\n");. The main body becomes run_quiz_app(); return 0;. The previous lesson's solution is in the starting code.
Exercise Write the run_quiz_app function
Write void run_quiz_app(void). In the body, in order: printf("Quiz hazır\n");, struct Question questions[10]; int count = 0;, two add_question calls ("Başkent?", 0 and "2+2?", 1), printf("%d soru eklendi\n", count);, int score = run_quiz(questions, count);, printf("Skor: %d\n", score);, print_percentage(score, count);, printf("Tamamlandı\n");. Make main's body run_quiz_app(); return 0;.
Show example solution
#include <stdio.h>
#include <string.h>
struct Question {
char text[64];
int correct;
};
void add_question(struct Question *q, int *count, const char *text, int correct) {
strcpy(q[*count].text, text);
q[*count].correct = correct;
(*count)++;
}
int run_quiz(struct Question *q, int count) {
int answers[3] = {0, 1, 2};
int score = 0;
for (int i = 0; i < count; i++) {
printf("Soru %d: %s\n", i + 1, q[i].text);
if (answers[i] == q[i].correct) {
score++;
}
}
return score;
}
void print_percentage(int score, int count) {
int pct = score * 100 / count;
printf("Yüzde: %d%%\n", pct);
}
void run_quiz_app(void) {
printf("Quiz hazır\n");
struct Question questions[10];
int count = 0;
add_question(questions, &count, "Başkent?", 0);
add_question(questions, &count, "2+2?", 1);
printf("%d soru eklendi\n", count);
int score = run_quiz(questions, count);
printf("Skor: %d\n", score);
print_percentage(score, count);
printf("Tamamlandı\n");
}
int main(void) {
run_quiz_app();
return 0;
}
Project complete
You've completed the five-lesson quiz app project. You started from the first lesson's skeleton; added the add_question, run_quiz and print_percentage functions; and finally combined them with run_quiz_app.
This structure is the foundation of real C projects: a data model (struct Question), layered functions and an integration point. In the next group you'll write and read data to disk with file I/O; this will let you persist quiz questions. The code you accumulated will serve as a skeleton for upcoming projects.
Test yourself
-
What's the role of run_quiz_app?
- Add questions
- Compute score
- Open a file
- Combine the whole flow
Show answer
Combine the whole flow — run_quiz_app combines the whole quiz flow in one function.
-
Match functions to role
- add_question
- Add questions
- run_quiz
- Run the quiz
- print_percentage
- Print percentage
- run_quiz_app
- Whole flow
-
Complete the run_quiz_app call
int main(void) {
____();
return 0;
}
Show answer
int main(void) {
run_quiz_app();
return 0;
}
Next lesson: FILE and fopen