C++ Unit Converter Project
Convert length, weight, and temperature between metric and imperial units.
What you will build
Pick a category, pick a direction, type a value, get a converted result. It sounds like pure arithmetic, but the real lesson is organising many small conversion functions behind a clean menu — and handling temperature, which refuses to follow the simple multiply-by-a-factor pattern.
What you will learn
- Writing one function per conversion
- Using const for fixed conversion factors
- Menu-driven program flow
- Formatting floating-point output
Feature checklist
- Length: metres ↔ feet, km ↔ miles
- Weight: kilograms ↔ pounds
- Temperature: Celsius ↔ Fahrenheit (formula, not factor)
- Menu to choose category and direction
- Rejects negative lengths/weights where they make no sense
Starter code
#include <iostream>
double cToF(double c) { return c * 9.0 / 5.0 + 32.0; }
double fToC(double f) { return (f - 32.0) * 5.0 / 9.0; }
double kmToMiles(double km) { return km * 0.621371; }
double kgToLb(double kg) { return kg * 2.20462; }
int main() {
std::cout << "1) C to F 2) F to C\n"
<< "3) km to miles 4) kg to lb\nChoice: ";
int choice; double v;
std::cin >> choice;
std::cout << "Value: ";
std::cin >> v;
switch (choice) {
case 1: std::cout << v << " C = " << cToF(v) << " F\n"; break;
case 2: std::cout << v << " F = " << fToC(v) << " C\n"; break;
case 3: std::cout << v << " km = " << kmToMiles(v) << " mi\n"; break;
case 4: std::cout << v << " kg = " << kgToLb(v) << " lb\n"; break;
default: std::cout << "Unknown choice.\n";
}
}Full walkthrough
Step 1: Write one small function per conversion
The starter's whole design is four one-line functions: cToF, fToC, kmToMiles, kgToLb. Each takes a double and returns a double, so the menu in main() never contains arithmetic — it only dispatches. When a conversion is wrong, you know the bug lives in exactly one line.
Step 2: Learn why temperature is the odd one out
Kilometres to miles is a pure factor (× 0.621371), but Celsius to Fahrenheit is c * 9.0 / 5.0 + 32.0 — a scale AND an offset, because the two scales do not share a zero. The checklist calls this out ('formula, not factor') because it is the one place a factor-table design breaks; write these two by hand and feel the difference.
double cToF(double c) { return c * 9.0 / 5.0 + 32.0; }
double fToC(double f) { return (f - 32.0) * 5.0 / 9.0; }Step 3: Use named constants, not magic numbers
kgToLb multiplies by 2.20462 and kmToMiles by 0.621371. In a bigger version you would declare these as constexpr doubles with names like LB_PER_KG — the point is that a reader should never have to guess what 2.20462 means or hunt through code to update it.
Step 4: Build the menu with a switch
main() prints the four options exactly as the starter does, reads an int choice, then a double value, and the switch calls the matching function and prints 'value unit = result unit'. The default case catches any other number and prints 'Unknown choice.' — a menu without a default case fails silently, which users read as a crash.
Step 5: Mind the order of operations in your formulas
Write c * 9.0 / 5.0, not c * (9 / 5): in C++, 9 / 5 is integer division and equals 1, which would make 100 °C 'convert' to 132 °F. Using 9.0 forces floating-point division. This single bug is responsible for more broken converter homework than everything else combined.
Step 6: Reject nonsense values
The checklist asks you to refuse negative lengths and weights: after reading the value, if it is below zero for a km or kg conversion, print an error and stop. Temperatures are the exception — negative Celsius is perfectly real weather, so do not apply the rule blindly to every branch.
Step 7: Test against values you can verify
Boiling water: 100 °C must give exactly 212 °F. Freezing: 0 °C → 32 °F. The crossover point -40 °C → -40 °F is a beautiful test because both formulas must agree there. For distance, 5 km → 3.106855 mi, and 10 kg → 22.0462 lb.
Common mistakes
- Writing 9 / 5 with integers and getting 1 — always make one side a double (9.0 / 5.0).
- Swapping cToF and fToC in the switch cases, so the menu lies about the direction of conversion.
- Applying the 'no negatives' rule to temperature and rejecting -10 °C, which is a legitimate input.
Compile and run
g++ -std=c++17 -o unit_converter unit_converter.cppSample run
$ ./unit_converter
1) C to F 2) F to C
3) km to miles 4) kg to lb
Choice:
> 1
Value:
> 100
100 C = 212 F
$ ./unit_converter
1) C to F 2) F to C
3) km to miles 4) kg to lb
Choice:
> 3
Value:
> 5
5 km = 3.10686 miTry it in your browser
This project has a browser demo in the classic site. The demo is a browser re-implementation of the same logic, not the compiled C++ program: Length, weight, and temperature conversions — including the temperature formulas that don't follow a simple factor.