#include <iostream>
#include <iomanip>
#include <map>
#include <string>
using namespace std;
class CurrencyConverter {
public:
CurrencyConverter();
void convertCurrency();
private:
map<string, double> exchangeRates;
void displayCurrencies();
double getExchangeRate(const string& fromCurrency, const string& toCurrency);
};
CurrencyConverter::CurrencyConverter() {
// Initialize exchange rates (example rates)
exchangeRates["USD"] = 1.0; // Base currency
exchangeRates["EUR"] = 0.85; // 1 USD = 0.85 EUR
exchangeRates["GBP"] = 0.75; // 1 USD = 0.75 GBP
exchangeRates["INR"] = 74.00; // 1 USD = 74.00 INR
exchangeRates["JPY"] = 110.00; // 1 USD = 110.00 JPY
}
void CurrencyConverter::displayCurrencies() {
cout << "Available Currencies:" << endl;
for (const auto& pair : exchangeRates) {
cout << pair.first << endl;
}
}
double CurrencyConverter::getExchangeRate(const string& fromCurrency, const string& toCurrency) {
return exchangeRates[toCurrency] / exchangeRates[fromCurrency];
}
void CurrencyConverter::convertCurrency() {
string fromCurrency, toCurrency;
double amount;
cout << "Welcome to the Currency Converter!" << endl;
displayCurrencies();
cout << "Enter the currency you want to convert from (e.g., USD): ";
cin >> fromCurrency;
cout << "Enter the currency you want to convert to (e.g., EUR): ";
cin >> toCurrency;
cout << "Enter the amount in " << fromCurrency << ": ";
cin >> amount;
if (exchangeRates.find(fromCurrency) != exchangeRates.end() &&
exchangeRates.find(toCurrency) != exchangeRates.end()) {
double rate = getExchangeRate(fromCurrency, toCurrency);
double convertedAmount = amount * rate;
cout << fixed << setprecision(2);
cout << amount << " " << fromCurrency << " is equal to "
<< convertedAmount << " " << toCurrency << endl;
} else {
cout << "Invalid currency entered. Please try again." << endl;
}
}
int main() {
CurrencyConverter converter;
char choice;
do {
converter.convertCurrency();
cout << "Do you want to perform another conversion? (y/n): ";
cin >> choice;
} while (choice == 'y' || choice == 'Y');
cout << "Thank you for using the Currency Converter!" << endl;
return 0;
}