Skip to content
C project #08Beginner

ATM Simulator

PIN verification, deposits, withdrawals with a daily limit and minimum balance, PIN change and a mini statement.

Estimated time
~3 hours to build
Files
5 files · 306 lines
Technologies
C11, <stdio.h>

Introduction

An ATM enforces many small rules: three PIN attempts, withdrawals in multiples of 100, a daily limit and a minimum balance. Implementing them teaches careful condition ordering and clear error messages.

Problem statement

Simulate an ATM for one account. Block the card after three wrong PINs. Offer balance enquiry, deposit, withdrawal (multiples of 100, daily limit Rs 20,000, minimum balance Rs 500), PIN change with confirmation, and a mini statement of the last five transactions.

Objectives

  • Enforce business rules with ordered conditions
  • Store recent transactions in a circular buffer
  • Use structs and compound literals
  • Split behaviour into small functions

Features

  • PIN check with 3 attempts
  • Deposit and withdrawal with all validation rules
  • Mini statement of the last 5 transactions, newest first
  • PIN change with confirmation and 4-digit check

Concepts demonstrated

  • Structs
  • Circular buffer
  • Validation rules
  • Static globals

System architecture

Account state is kept in file-scope static variables. Each menu option is a function. Transactions are stored in a fixed array of 5 Txn structs used as a circular buffer: history_next is where the next record goes and wraps with % HISTORY; the statement walks backwards from the newest entry.

Folder structure

TextProject tree
atm-simulator/
├── README.md
├── Makefile
├── 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
Account state, verify_pin, deposit, withdraw (rule checks in order), change_pin, the circular-buffer record/statement, and the menu loop.
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
=== KENHOLT Bank ATM ===
Enter PIN:
Incorrect PIN (2 attempt(s) left).
Enter PIN:
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Available balance: Rs 5000.00
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Amount to deposit:
Deposited Rs 2500.00. New balance: Rs 7500.00
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Amount to withdraw (multiples of 100):
Amount must be a positive multiple of 100.
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Amount to withdraw (multiples of 100):
Please collect Rs 1000.00. New balance: Rs 6500.00
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Amount to withdraw (multiples of 100):
Insufficient funds: a minimum balance of Rs 500 is required.
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Mini statement (most recent first):
Withdrawal 1000.00 balance 6500.00
Deposit 2500.00 balance 7500.00
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
New 4-digit PIN:
Confirm new PIN:
PIN changed successfully.
1. Balance 2. Deposit 3. Withdraw 4. Mini statement 5. Change PIN 0. Exit
Choice:
Thank you for banking with us.
Input
1111
1234
1
2
2500
3
750
3
1000
3
7000
4
5
4321
4321
0

Testing

  • Compare with tests/expected-output.txt: one wrong PIN, a deposit, an invalid amount (750), a valid withdrawal, a withdrawal blocked by the minimum balance, the statement and a PIN change.
  • Enter three wrong PINs and confirm the card is blocked.
  • Withdraw repeatedly to hit the Rs 20,000 daily limit.

Common errors

Statement shows transactions in the wrong order

Walk backwards: index = (history_next − 1 − i + HISTORY) % HISTORY.

Amounts like 150.5 pass the multiple-of-100 check

Also require that the amount equals its integer part before using % 100.

Possible improvements

  • Persist the account to a file so the balance survives restarts
  • Support multiple accounts with card numbers
  • Add a fund transfer option

Additional challenges

  1. 1Store the PIN as a hash instead of plain text.
  2. 2Reset the daily limit when the date changes (use <time.h>).
  3. 3Add a denomination breakdown (how many 500/200/100 notes are dispensed).

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