C++ Currency Converter Project
Convert between world currencies using a built-in table of exchange rates.
What you will build
Convert amounts between currencies using rates stored in a lookup table. The key insight: instead of a switch full of hard-coded pairs, store every rate against one base currency in a std::map — then any-to-any conversion is a single formula, and adding a currency is adding one line.
What you will learn
- Using std::map as a lookup table
- Converting via a common base currency
- Validating keys before use
- Rounding money to two decimals
Feature checklist
- At least 6 currencies (USD, EUR, GBP, NGN, JPY, INR)
- Convert any currency to any other
- Rates stored relative to USD
- Clear error for unknown currency codes
- Output rounded to 2 decimal places
Starter code
#include <iostream>
#include <map>
#include <string>
#include <iomanip>
int main() {
// Rate = how many units 1 USD buys
std::map<std::string, double> rate = {
{"USD", 1.0}, {"EUR", 0.92}, {"GBP", 0.79},
{"NGN", 1550.0}, {"JPY", 149.5}, {"INR", 83.2}
};
std::string from, to; double amount;
std::cout << "From (e.g. USD): "; std::cin >> from;
std::cout << "To (e.g. NGN): "; std::cin >> to;
std::cout << "Amount: "; std::cin >> amount;
if (!rate.count(from) || !rate.count(to)) {
std::cout << "Unknown currency code.\n";
return 1;
}
double inUsd = amount / rate[from];
double result = inUsd * rate[to];
std::cout << std::fixed << std::setprecision(2)
<< amount << " " << from << " = " << result << " " << to << "\n";
}Full walkthrough
Step 1: Store rates in a map against one base currency
The design decision that makes this project ten lines instead of a hundred: std::map<std::string, double> rate holds how many units 1 USD buys — {"USD", 1.0}, {"EUR", 0.92}, {"NGN", 1550.0} and so on. You never store USD→NGN AND NGN→USD AND EUR→NGN; the base currency does all the work.
Step 2: Convert via the base with one formula
Any-to-any conversion is two steps: inUsd = amount / rate[from], then result = inUsd * rate[to]. Dividing by the 'from' rate climbs up to dollars; multiplying by the 'to' rate climbs back down. Once you see it, you can add a new currency by adding ONE map entry — no new code paths, no new bugs.
double inUsd = amount / rate[from];
double result = inUsd * rate[to];Step 3: Validate the currency codes before touching the map
Check rate.count(from) and rate.count(to) first and print 'Unknown currency code.' if either is missing. This check is not paranoia: operator[] on a std::map silently CREATES a missing entry with value 0.0, and dividing by that invented zero produces inf instead of an error. count() (or find()) is the safe way to peek.
Step 4: Read the three inputs in a friendly order
Prompt 'From (e.g. USD): ', 'To (e.g. NGN): ', then 'Amount: '. Reading codes as std::string with std::cin >> works because currency codes contain no spaces. Decide on case: the starter expects uppercase, so either say so in the prompt or normalise the input to uppercase yourself — silently failing on 'usd' is a paper cut users will report as 'broken'.
Step 5: Format money to exactly two decimals
Print with std::fixed << std::setprecision(2) from <iomanip>: '100.00 USD = 155000.00 NGN'. Currencies like JPY have no minor unit in practice, but two decimals everywhere keeps the output consistent and the code simple — a deliberate simplification worth naming.
Step 6: Test round-trips and the identity case
Convert 100 USD → NGN (155000.00), then convert that result back NGN → USD and demand 100.00 again; round-trip drift means a rate was typed inconsistently. Also test USD → USD, which must return the amount unchanged — the formula handles it for free, and if it does not, your formula is wrong.
Common mistakes
- Looking up rates with rate[from] before validating, which inserts a phantom 0.0 rate and turns errors into infinities.
- Hard-coding a switch of currency pairs — six currencies is 30 pairs; the base-currency formula replaces all of them.
- Forgetting std::fixed, so setprecision(2) can print large amounts in scientific notation.
Compile and run
g++ -std=c++17 -o currency currency.cppThe rates in the starter code are fixed example values typed into the program — they are not live exchange rates. Update the numbers in the rate table by hand whenever you want fresher ones.
Sample run
$ ./currency
From (e.g. USD):
> USD
To (e.g. NGN):
> NGN
Amount:
> 100
100.00 USD = 155000.00 NGN
$ ./currency
From (e.g. USD):
> USD
To (e.g. NGN):
> XXX
Amount:
> 10
Unknown currency code.Try it in your browser
No browser demo is included for this project. Build the C++ version below — that is the real program.