Skip to content
C++ project #08Beginner

Roman Numeral Converter

Convert numbers to Roman numerals and back with strict validation, show how each numeral is built, and add or subtract in Roman.

Estimated time
~2 hours to build
Files
5 files · 273 lines
Technologies
C++17, <array>, <optional>, <algorithm>

Introduction

Converting to Roman numerals is easy; validating them is the real exercise. This converter rejects non-canonical forms such as IIII, VX or IC with one elegant trick: decode the numeral, re-encode the value, and require the two to match.

Problem statement

Read one item per line. A number from 1 to 3999 is converted to a Roman numeral, with a breakdown of its parts. A Roman numeral (any letter case) is converted to a number if it is in canonical form. Lines of the form add X Y or sub X Y (Roman or decimal operands) print the result in Roman numerals when it is in range. Report rejected items and a final count.

Objectives

  • Use a lookup table of value/symbol pairs
  • Apply the subtractive rule when decoding
  • Validate by round-tripping a conversion
  • Return 'maybe a value' with std::optional

Features

  • Decimal → Roman with a breakdown (M + CM + XC + IV)
  • Roman → decimal, case-insensitive
  • Rejects non-canonical and out-of-range input
  • Roman addition and subtraction

Concepts demonstrated

  • Lookup tables
  • Greedy algorithms
  • std::optional
  • Validation by round trip
  • std::transform

System architecture

TABLE lists the 13 value/symbol pairs from 1000 down to 1, including subtractive pairs such as 900/CM. to_roman is greedy: it repeatedly takes the largest value that fits. from_roman upper-cases the text, sums the symbols (subtracting a symbol smaller than its right neighbour), then returns std::nullopt unless to_roman(total) reproduces the input exactly. parse accepts either form, so the arithmetic commands work with mixed operands.

Folder structure

TextProject tree
roman-numeral-converter/
├── README.md
├── Makefile
├── src/main.cpp
├── 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
CXX = g++
CXXFLAGS = -std=c++17 -Wall -Wextra -O2
SRC := $(wildcard src/*.cpp)
HDR := $(wildcard src/*.h src/*.hpp)
ARGS := $(shell cat tests/args.txt 2>/dev/null)
INPUT := $(wildcard tests/sample-input.txt)
app: $(SRC) $(HDR)
$(CXX) $(CXXFLAGS) -Isrc $(SRC) -o app
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.cpp
The symbol table, to_roman, from_roman (with round-trip validation), parse, explain and the line 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
g++ -std=c++17 -Wall -Wextra -Isrc src/*.cpp -o app
# 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
1994 -> MCMXCIV [M + CM + XC + IV]
2026 -> MMXXVI [M + M + X + X + V + I]
3999 -> MMMCMXCIX [M + M + M + CM + XC + IX]
4000 -> error: only 1 to 3999 can be converted
0 -> error: only 1 to 3999 can be converted
MCMXCIV -> 1994
mmxxvi -> 2026
XLII -> 42
IIII -> error: not a valid (canonical) Roman numeral
VX -> error: not a valid (canonical) Roman numeral
IC -> error: not a valid (canonical) Roman numeral
MMMM -> error: not a valid (canonical) Roman numeral
hello -> error: not a valid (canonical) Roman numeral
XIV + XXVIII = XLII (42)
sub X XII -> error: result -2 cannot be written in Roman numerals
add MMM M -> error: result 4000 cannot be written in Roman numerals
7 converted, 9 rejected
Input
1994
2026
3999
4000
0
MCMXCIV
mmxxvi
XLII
IIII
VX
IC
MMMM
hello
add XIV 28
sub X XII
add MMM M

Testing

  • Compare ./app < tests/sample-input.txt with tests/expected-output.txt.
  • 1994 ↔ MCMXCIV, 2026 ↔ MMXXVI and 3999 ↔ MMMCMXCIX are checked both ways.
  • IIII, VX, IC and MMMM must all be rejected; so must 0 and 4000.

Common errors

"IIII" is accepted as 4

Summing symbols alone isn't enough; compare the numeral with to_roman(value).

std::out_of_range from std::stoi on a long number

Reject digit strings longer than four characters before calling stoi, as parse does.

Possible improvements

  • Support the vinculum (overline) notation for numbers above 3999
  • Add multiplication and integer division
  • Convert dates such as 2026-11-27 into Roman form

Additional challenges

  1. 1Write from_roman without the round-trip check, using explicit rules.
  2. 2Print a table of 1–100 in five columns.
  3. 3Count how many numerals from 1 to 3999 contain each symbol.

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