Skip to main content

How to Create a Simple Flashcard Quiz System in Batch Script

Creating a flashcard quiz system is a practical way to learn coding concepts while building a useful tool for personal study. Windows Batch Script allows you to create a surprisingly effective interactive quiz system that can track your score, provide feedback, and randomize questions. This project is perfect for entry-to-mid-level developers who want to practice data organization and conditional logic.

In this guide, we will go through the process of building a quiz engine that uses pseudo-arrays to store questions and answers, and handles user input to determine progress.

Designing the System Architecture

A robust flashcard system requires three main components:

  1. A Data Store: A place where your questions and their corresponding answers are defined.
  2. A Randomizer Engine: To ensure you aren't always answering questions in the same order.
  3. An Evaluation Logic: To compare the user's input with the correct answer and update the score.

Key Batch Concepts Used

  • Variable Substitution: Dynamically accessing variables using call set to resolve nested references.
  • Logical Conditionals: Using IF statements to check correctness.
  • Looping: Using FOR /L and :labels to repeat the quiz process.

Step 1: Defining the Flashcard Data

Since Batch doesn't support complex data structures like JSON, we use environment variables with numeric suffixes. It is crucial to keep the numbering consistent between the question and the answer.

set "q1=What does CPU stand for?"
set "a1=Central Processing Unit"

set "q2=What is the file extension for a Windows Batch file?"
set "a2=.bat"

set "q3=Which command is used to display text on the screen?"
set "a3=echo"
Admonition: Scaling

When adding more questions, remember to update your "total questions" variable. This ensures the randomizer knows the new range of possible cards.

Step 2: Implementing the Quiz Logic

One of the most important aspects of a flashcard system is providing clear feedback. Simply telling a user they are "wrong" isn't helpful; showing the correct answer is vital for learning.

The Wrong Way: Case-Sensitive Hard Comparison

A common mistake is forgetting that user input might vary in capitalization.

:: WRONG WAY
set /p "user_ans=Answer: "
if "%user_ans%"=="%a1%" (
echo Correct!
) else (
echo Wrong!
)

Output Concern: If the answer is "echo" and the user types "ECHO", the script will tell them they are wrong. This is frustrating and technically incorrect for a quiz system.

The Correct Way: Case-Insensitive Comparison

Using the /i switch with the IF command makes the comparison much more forgiving. Combining this with delayed expansion protects against special characters in user input.

if /i "!user_ans!"=="!a1!" (
echo Correct!
)

Step 3: Adding Randomization

To make the quiz challenging, we use the %RANDOM% variable to pick a random question ID.

set /a "q_idx=(%RANDOM% %% total_questions) + 1"

The Complete Flashcard Quiz Script

Below is a fully functional script with score tracking and a clean interface.

@echo off
title Batch Flashcard Quiz
setlocal enabledelayedexpansion

:: --- Configuration and Data ---
set "total_q=5"
set "score=0"
set "attempts=0"

set "q1=What command clears the console screen?"
set "a1=cls"
set "q2=What variable contains a random number between 0 and 32767?"
set "a2=%%RANDOM%%"
set "q3=Which command is used to jump to a label?"
set "a3=goto"
set "q4=How do you start a comment line in Batch?"
set "a4=rem"
set "q5=What does GUI stand for?"
set "a5=Graphical User Interface"

:menu
cls
color 0F
echo ===========================================
echo BATCH FLASHCARD QUIZ
echo ===========================================
echo Questions in deck: %total_q%
echo Current Score: !score! / !attempts!
echo ===========================================
echo.
echo [1] Start Random Question
echo [2] Reset Score
echo [3] Exit
echo.
set "m_choice="
set /p "m_choice=Select an option: "

if "!m_choice!"=="1" goto quiz
if "!m_choice!"=="2" set "score=0" & set "attempts=0" & goto menu
if "!m_choice!"=="3" exit /b
goto menu

:quiz
:: Pick a random question
set /a "idx=(!RANDOM! %% %total_q%) + 1"
call set "current_q=%%q!idx!%%"
call set "current_a=%%a!idx!%%"

cls
echo ===========================================
echo QUESTION #!idx!
echo ===========================================
echo.
echo !current_q!
echo.
echo ===========================================
set "u_ans="
set /p "u_ans=Your Answer: "

set /a "attempts+=1"

if /i "!u_ans!"=="!current_a!" (
color 0A
echo.
echo [CORRECT] Well done.
set /a "score+=1"
pause
goto menu
) else (
color 0C
echo.
echo [WRONG] The correct answer was: !current_a!
echo.
pause
goto menu
)

Explaining the Script Mechanics

  • color 0A and color 0C: These commands briefly change the background/foreground colors. This provides strong visual cues for "Pass" (Green) and "Fail" (Red).
  • call set "current_q=%%q!idx!%%": This resolves the nested variable reference in two phases. Delayed expansion first resolves !idx! to a number (e.g., 3), then the call forces a second parse pass that resolves the resulting %q3% into the stored question text.
  • Persistent Scores: The score variables are maintained as long as the script is running, allowing the user to see their accuracy over time.

Best Practices for Quiz Systems

  1. Clear Layout: Use dashes or equals signs to separate the question from the input area.
  2. Input Clearing: Always use set "var=" before set /p to ensure that pressing Enter without typing is detected as empty input, since set /p does not modify the variable when no input is provided.
  3. Label Jump Optimization: Use :labels to keep the menu, the question logic, and the feedback sections distinct.

Conclusion

Building a flashcard system in Batch is a productive way to practice scripting. It covers the essential pillars of programming: data storage, user interaction, randomization, and conditional evaluation. Once you have this basic system running, you can easily expand it by adding more questions or even teaching the script to read question data from an external text file.