Unit & Temperature Converter CLI
Build a unit converter for the console in C: choose a category (temperature, length or weight), pick the units, type a value, and see the result, again and again until you quit, with a short history of your recent conversions. Instead of a long chain of if and else, the conversions are organized in tables of function pointers and conversion factors, a neat C technique. Every input is checked, so typing letters or impossible values never crashes the program. It's a good first C project: you'll learn functions and function pointers, arrays of structs, switch statements, reading input safely with fgets and strtod, a circular buffer for history, and building with a Makefile.
- Estimate
- ~7h
- Steps
- 6
- Completed by
- 0
- Proposed by
- codeseed.app
stdio.h · math.h
Project roadmap
- 01
Design the menus and data
~1hWrite a struct for a unit (its name, symbol and factor to the base unit) and arrays of units for length (base: meters) and weight (base: kilograms). Temperature needs formulas, so plan a small set of functions for it. Print a main menu listing the categories and a quit option. Set up a Makefile with -Wall -Wextra. Done when: the menu prints and the program exits on quit.
- 02
Write the conversion functions
~1.5hWrite a generic function that converts length or weight through the base unit using the factors, and functions for temperature (Celsius to Fahrenheit and Kelvin and back), refusing temperatures below absolute zero. Done when: 5 km to miles gives 3.107, 100 °C gives 212 °F, and -300 °C is refused.
- 03
Dispatch with function pointers
~1.5hBuild an array of structs, one per category, holding the category name and a pointer to a function that runs that category's conversion menu, and call the chosen entry through its pointer instead of using a long if and else chain. Done when: choosing each category from the menu calls the right function through the table.
- 04
Validate input
~1hRead every input line with fgets, then parse numbers with strtod and menu choices with strtol, checking that the whole line was used (no extra characters), the number is finite, and the choice is in range. Re-ask with a clear message on invalid input, and handle the end of input (Ctrl+D) by exiting cleanly. Done when: letters, empty lines and out-of-range choices never crash the program.
- 05
Keep a conversion history
~1hStore the last 10 conversions (value, units and result) in a circular buffer (a fixed array with a next-position index), and add a menu option to show them, oldest first. Done when: the history shows the right conversions in order, even after more than 10.
- 06
Test it
~1hWrite a small test program with assert for the conversion functions (every category, round trips and absolute zero) and the circular buffer. Add a script that feeds input to the program through a pipe and checks its output. Done when: the tests pass, and valgrind reports no errors.
Ready to build this?
Get a GitHub repo and start building. Your AI reviewer checks each step as you go.
Tech stack