Kelick Tech Hub

Build, learn & keep up — tech, Linux & sports news included.

C++ Currency Converter Project

Beginner•2–3 hours•std::map, functions, lookup tables

Convert between world currencies using a built-in table of exchange rates.

Share on X

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

Feature checklist

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

Compile and run

g++ -std=c++17 -o currency currency.cpp

The 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.

Related Beginner projects

← All 60 projects · Classic site (Learn & Sandbox)

Download Fake GPS