Skip to content
C project #13Intermediate

Quiz Application

Questions loaded from a text file, shuffled with Fisher–Yates, scored by topic, with a review of every wrong answer.

Estimated time
~5 hours to build
Files
6 files · 412 lines
Technologies
C11, Text parsing, Command-line arguments

Introduction

Separating data from code is a key software-engineering idea. Here the questions live in an editable text file; the program parses it, randomises the order reproducibly, and produces a scored report with per-topic results.

Problem statement

Load multiple-choice questions from questions.txt, ask a chosen number of them in shuffled order, accept answers A–D (re-prompting on invalid input), and finish with the score, a per-topic breakdown and a review of incorrect answers.

Objectives

  • Parse a line-based text format into structs
  • Implement the Fisher–Yates shuffle
  • Aggregate results by category
  • Accept a file name from the command line

Features

  • Editable question bank with topics
  • Reproducible shuffling from a seed
  • Input re-prompting
  • Score, per-topic breakdown and answer review

Concepts demonstrated

  • Parsing
  • Shuffling algorithms
  • Arrays of structs
  • Aggregation

System architecture

load reads the file line by line, filling a Question as it sees Q:, T:, A:–D: and ANS: lines; a question is accepted only when all parts are present. An index array is shuffled with Fisher–Yates using a seeded LCG. Topic totals are kept in parallel arrays.

Folder structure

TextProject tree
quiz-application/
├── README.md
├── Makefile
├── questions.txt
├── src/main.c
├── tests/expected-output.txt
└── tests/sample-input.txt

Complete source code

Makefile

20 lines
MakefileMakefile
# Generated for KENHOLT Skills. Usage: make | make test | make clean
CC = gcc
CFLAGS = -std=c11 -Wall -Wextra -O2
SRC := $(wildcard src/*.c)
HDR := $(wildcard src/*.h)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CC) $(CFLAGS) -Isrc $(SRC) -o app -lm
test: app
./app $(ARGS) $(if $(INPUT),< $(INPUT),) > actual.txt
diff actual.txt tests/expected-output.txt && echo "Test passed: output matches tests/expected-output.txt"
clean:
rm -f app app.exe actual.txt
.PHONY: test clean

What each file does

src/main.c
File parser, LCG + Fisher–Yates shuffle, the quiz loop and the result report.
Makefile
Build automation: make builds ./app, make test runs the sample session and compares it with the expected output, make clean removes build files.

How to run it

Build

Terminal
gcc -std=c11 -Wall -Wextra -Isrc src/*.c -o app -lm
# or simply: make

Run

Terminal
./app # Linux / macOS
.\app.exe # Windows

Test against the expected output

Terminal
./app < tests/sample-input.txt > actual.txt
diff actual.txt tests/expected-output.txt # no output means they match
# Windows PowerShell:
# Get-Content tests\sample-input.txt | .\app.exe > actual.txt
# Compare-Object (Get-Content actual.txt) (Get-Content tests\expected-output.txt)

Expected output

Generated by running the project with tests/sample-input.txt. Your output should match tests/expected-output.txt exactly.

Texttests/expected-output.txt
Loaded 10 questions.
Seed for question order:
How many questions (1-10):
Q1. [Strings] Which header declares strlen?
A) <string.h>
B) <stdio.h>
C) <stdlib.h>
D) <ctype.h>
Your answer (A-D):
Wrong - the answer is A.
Q2. [Memory] Which function releases memory allocated with malloc?
A) delete
B) release
C) free
D) dealloc
Your answer (A-D):
Correct!
Q3. [Pointers] In int *p = arr; what does *(p + 2) refer to?
A) arr[2]
B) arr[0] + 2
C) the address of arr
D) arr[1]
Your answer (A-D):
Your answer (A-D):
Correct!
Q4. [Strings] What value terminates every C string?
A) '\n'
B) '\0'
C) ' '
D) EOF
Your answer (A-D):
Wrong - the answer is B.
Q5. [Basics] What does sizeof(char) always evaluate to?
A) 1
B) 2
C) 4
D) It depends on the compiler
Your answer (A-D):
Wrong - the answer is A.
===== Result =====
Score: 2 / 5 (40%)
Strings 0/2
Memory 1/1
Pointers 1/1
Basics 0/1
Review:
Q1 Which header declares strlen?
you: <stdio.h> | correct: <string.h>
Q4 What value terminates every C string?
you: EOF | correct: '\0'
Q5 What does sizeof(char) always evaluate to?
you: 2 | correct: 1
Input
7
5
B
C
x
A
D
B

Testing

  • Diff with tests/expected-output.txt (seed 7, 5 questions, including an invalid answer "x").
  • Add a new question to questions.txt and confirm it is loaded.
  • Delete the ANS line of one question and check that it is skipped.

Common errors

"Could not load questions"

Run the program from the project folder, or pass the file path: ./app path/to/questions.txt.

A question is silently skipped

Each question needs exactly the lines Q, T, A, B, C, D and ANS in that order.

Possible improvements

  • Timed questions
  • Negative marking
  • Save high scores

Additional challenges

  1. 1Shuffle the options of each question too (and track the new correct letter).
  2. 2Support questions with code snippets spanning several lines.
  3. 3Read the question bank in CSV format instead.

Built and tested it yourself? Mark it to update your dashboard.