1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 |
#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; } |
Explanation:
- Class Definition (
CurrencyConverter
):- The
CurrencyConverter
class manages the exchange rates and handles the conversion process.
- The
- Exchange Rates Initialization:
- The constructor initializes a
map
of exchange rates, where each currency code (e.g., “USD”, “EUR”) maps to its exchange rate relative to a base currency (USD in this case).
- The constructor initializes a
- Currency Display (
displayCurrencies
):- The
displayCurrencies
method prints out the list of available currencies to the user.
- The
- Exchange Rate Calculation (
getExchangeRate
):- The
getExchangeRate
method calculates the conversion rate from one currency to another using the formula:rate = exchangeRates[toCurrency] / exchangeRates[fromCurrency]
- The
- Currency Conversion (
convertCurrency
):- The
convertCurrency
method prompts the user to input the source currency, target currency, and amount. - It then calculates and displays the converted amount using the exchange rate provided by
getExchangeRate
.
- The
- Main Program Loop:
- The
main
function initializes aCurrencyConverter
object and allows the user to perform multiple conversions in a loop, until they choose to exit by entering ‘n’.
- The
Possible Enhancements:
- Dynamic Exchange Rates: Implement a system to fetch real-time exchange rates from an online API.
- Support for More Currencies: Expand the list of available currencies.
- Graphical Interface: Develop a GUI for the converter to enhance user interaction.
- Error Handling: Improve error handling for invalid inputs or unsupported currency codes.