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
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:
- 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. - Create a C++ File: Copy the provided C++ code into a file named
CurrencyConverter.cpp
. - 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
- Run the Program: After compiling, run the program by typing:
./CurrencyConverter
- 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.