cplusplus
cplusplus

 

Introduction

In today’s globalized world, currency conversion plays a vital role in financial transactions, trade, and tourism. A Currency Converter program allows users to convert one currency to another based on the current exchange rates. This program helps automate the tedious process of manual conversion and provides an easy-to-use solution for individuals and businesses alike.

Objective

The objective of this program is to develop a simple currency converter that can convert amounts between different currencies. It will take an input amount in one currency and convert it to the equivalent amount in a target currency using predefined exchange rates. The user will be prompted to select the currencies they want to convert between and enter the amount to be converted.

C++ Code for Currency Converter

#include 
#include 

#include 

using namespace std;

class CurrencyConverter {
private:
    map<string, double> exchangeRates;

public:
    // Constructor to initialize some common exchange rates
    CurrencyConverter() {
        exchangeRates["USD"] = 1.0;  // Base currency is USD
        exchangeRates["EUR"] = 0.85; // 1 USD = 0.85 EUR
        exchangeRates["GBP"] = 0.75; // 1 USD = 0.75 GBP
        exchangeRates["INR"] = 74.58; // 1 USD = 74.58 INR
        exchangeRates["JPY"] = 113.57; // 1 USD = 113.57 JPY
        exchangeRates["AUD"] = 1.35; // 1 USD = 1.35 AUD
    }

    // Method to convert the amount from one currency to another
    double convert(string fromCurrency, string toCurrency, double amount) {
        if (exchangeRates.find(fromCurrency) == exchangeRates.end() || exchangeRates.find(toCurrency) == exchangeRates.end()) {
            cout << "Invalid currency code!" << endl;
            return -1;
        }

        // Convert the amount to USD first, then to the target currency
        double amountInUSD = amount / exchangeRates[fromCurrency];
        double convertedAmount = amountInUSD * exchangeRates[toCurrency];
        return convertedAmount;
    }

    // Method to display the available currencies
    void displayCurrencies() {
        cout << "Available currencies: ";
        for (const auto& rate : exchangeRates) {
            cout << rate.first << " ";
        }
        cout << endl;
    }
};

int main() {
    CurrencyConverter converter;
    string fromCurrency, toCurrency;
    double amount;

    // Display available currencies
    converter.displayCurrencies();

    // Get user input
    cout << "Enter the currency to convert from: "; cin >> fromCurrency;
    cout << "Enter the currency to convert to: "; cin >> toCurrency;
    cout << "Enter the amount to convert: "; cin >> amount;

    // Perform conversion
    double result = converter.convert(fromCurrency, toCurrency, amount);
    if (result != -1) {
        cout << amount << " " << fromCurrency << " = " << result << " " << toCurrency << endl;
    }

    return 0;
}
        

Program Explanation

This Currency Converter program is designed using the C++ programming language and involves the use of basic classes, functions, and the map container from the C++ Standard Library. Let’s break down the structure of the program:

1. CurrencyConverter Class

The program defines a class called CurrencyConverter that holds exchange rates for several currencies. It initializes a map of exchange rates where the key is the currency code (e.g., “USD”, “EUR”) and the value is the corresponding exchange rate relative to USD (which is the base currency).

The class has two main functions:

  • convert(): This function converts an amount from one currency to another. First, it checks if the provided currency codes are valid. If valid, it converts the given amount to USD, and then converts it to the target currency using the exchange rate.
  • displayCurrencies(): This function simply prints out all the available currencies in the system.

2. Main Function

The main() function creates an instance of the CurrencyConverter class and interacts with the user. It prompts the user for input (the source currency, the target currency, and the amount to convert), then it calls the convert() method to perform the conversion and displays the result.

3. Error Handling

If the user enters an invalid currency code (one that is not in the map of exchange rates), the program will display an error message and return -1 as the result. This ensures that the user gets feedback on incorrect input.

How to Run the Program

To run this program on your local machine, follow these steps:

  1. Install a C++ Compiler: Ensure that you have a C++ compiler installed on your system. For Windows, you can use MinGW or Visual Studio. For Linux or macOS, the g++ compiler is commonly available.
  2. Create a C++ File: Copy the provided C++ code into a file named CurrencyConverter.cpp.
  3. Compile the Program: Open a terminal or command prompt and navigate to the directory where your C++ file is saved. Then, compile the program using the following command:
    g++ CurrencyConverter.cpp -o CurrencyConverter
  4. Run the Program: After compiling, run the program by typing:
    ./CurrencyConverter
  5. Input Data: The program will ask for the currencies and the amount to convert. Enter the details and the program will show you the converted amount.
Learn Programming – Copyright © 2024

 

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 :)