cplusplus
cplusplus

 

 

Track and Convert Currency Exchange Rates with C++

Introduction

The Currency Exchange App is designed to allow users to track and convert currencies based on real-time exchange rates. It allows users to input an amount in one currency and get its equivalent value in another currency, taking into account the current exchange rate.

This app provides a simple solution to currency conversion, making it easier for people to manage their financial transactions when traveling or dealing with foreign currencies.

Objective

The primary objective of this app is to provide users with a tool that can perform currency conversion based on the latest exchange rates. It can be extended by integrating APIs for real-time data or even enhanced with a graphical interface for better usability.

Code Implementation (C++)

#include 
#include 

#include 

using namespace std;

// Function to display available currencies and their codes
void displayCurrencies() {
    cout << "Available Currencies: \n";
    cout << "1. USD (US Dollar)\n";
    cout << "2. EUR (Euro)\n";
    cout << "3. GBP (British Pound)\n";
    cout << "4. INR (Indian Rupee)\n";
    cout << "5. JPY (Japanese Yen)\n";
}

// Function to perform currency conversion
double convertCurrency(double amount, double rate) {
    return amount * rate;
}

int main() {
    // Define exchange rates relative to USD
    map<string, double> exchangeRates;
    exchangeRates["EUR"] = 0.93;  // 1 USD = 0.93 EUR
    exchangeRates["GBP"] = 0.80;  // 1 USD = 0.80 GBP
    exchangeRates["INR"] = 83.13; // 1 USD = 83.13 INR
    exchangeRates["JPY"] = 133.47; // 1 USD = 133.47 JPY

    double amount;
    string fromCurrency, toCurrency;

    // Display available currencies
    displayCurrencies();

    // User input
    cout << "\nEnter the amount to convert: "; cin >> amount;

    cout << "\nEnter the source currency code (e.g., USD, EUR): "; cin >> fromCurrency;

    cout << "Enter the target currency code (e.g., USD, EUR): "; cin >> toCurrency;

    // Check if the entered currencies are valid
    if (exchangeRates.find(fromCurrency) != exchangeRates.end() && exchangeRates.find(toCurrency) != exchangeRates.end()) {
        double conversionRate = exchangeRates[toCurrency] / exchangeRates[fromCurrency];
        double convertedAmount = convertCurrency(amount, conversionRate);

        // Output the conversion result
        cout << "\n" << amount << " " << fromCurrency << " is equivalent to " << convertedAmount << " " << toCurrency << endl;
    } else {
        cout << "\nInvalid currency code entered. Please check and try again.\n";
    }

    return 0;
}
    

Explanation of the Program

This C++ program allows users to input a currency amount and convert it from one currency to another using pre-defined exchange rates. The program provides a basic example of how to implement currency conversion.

The key components of the program are:

  • Exchange Rates: A map of exchange rates is used to define the value of different currencies relative to USD.
  • Conversion Logic: The conversion is done by multiplying the input amount with the appropriate exchange rate.
  • User Input: The user inputs the amount to convert, along with the source and target currencies. The program checks if the currency codes are valid before performing the conversion.

How to Run the Program

Follow these steps to run the Currency Exchange App:

  1. Ensure you have a C++ compiler installed (e.g., GCC, Clang, or MinGW).
  2. Save the code to a file named CurrencyExchange.cpp.
  3. Open a terminal/command prompt and navigate to the directory where the file is saved.
  4. Compile the program using the following command:
    g++ CurrencyExchange.cpp -o CurrencyExchange
  5. Run the program using:
    ./CurrencyExchange
  6. Follow the on-screen prompts to enter the amount and currency codes for conversion.
© 2025 Learn Programming. All Rights Reserved.

 

By Aditya Bhuyan

I work as a cloud specialist. In addition to being an architect and SRE specialist, I work as a cloud engineer and developer. I have assisted my clients in converting their antiquated programmes into contemporary microservices that operate on various cloud computing platforms such as AWS, GCP, Azure, or VMware Tanzu, as well as orchestration systems such as Docker Swarm or Kubernetes. For over twenty years, I have been employed in the IT sector as a Java developer, J2EE architect, scrum master, and instructor. I write about Cloud Native and Cloud often. Bangalore, India is where my family and I call home. I maintain my physical and mental fitness by doing a lot of yoga and meditation.

Leave a Reply

Your email address will not be published. Required fields are marked *

error

Enjoy this blog? Please spread the word :)