CodeBlanks Logo Icon CodeBlanks

Putting it together

  • C course
  • Intermediate
  • Lesson 46/116
  • 6 min

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.

Practice 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;.

Starter code

main.c
#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);
}

// Write the run_quiz_app function: move the whole flow into it
// Add "Tamamlandı" at the end
// Replace main body with a run_quiz_app call

int main(void) {
    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);
    return 0;
}

Sample solution

main.c
#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;
}

Expected output

Quiz hazır
2 soru eklendi
Soru 1: Başkent?
Soru 2: 2+2?
Skor: 2
Yüzde: 100%
Tamamlandı

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.

Questions and answers

Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.

1 What's the role of run_quiz_app?

  • Add questions
  • Compute score
  • Open a file
  • Combine the whole flow Correct answer

Explanation: run_quiz_app combines the whole quiz flow in one function.

2 Match functions to role

Term Meaning
add_question Add questions
run_quiz Run the quiz
print_percentage Print percentage
run_quiz_app Whole flow

3 Complete the run_quiz_app call

Question
int main(void) {
    ____();
    return 0;
}
Answer
int main(void) {
    run_quiz_app();
    return 0;
}

Explanation: main calls the run_quiz_app function.

All lessons in Intermediate