'Is there an efficient way to check which currency my user wants to convert?
I am wondering how I could check which currencies the user wants to convert, without making hundreds of "if" statements. Would this have worked better if I had stored the currencies in an array? I have no idea how to go about this, and would greatly appreciate any help.
#include <iostream>
#include <string>
using namespace std;
int choice;
int choice2;
int main()
{
cout << "Hello! Welcome to this converter! If you want to see all possible conversions, type 1. If not, type 0 to "
"skip straight to the conversions!";
cin >> choice;
cout << "Your choice: " << choice << endl;
/*
cin >> choice;
cin >> choice2;
cin >> choice3;
*/
if (choice == 1)
{
cout << "Major World Currencies: United States Dollar, $ (USD) Euro, € (EUR) BrEuropitish Pound Sterling, £ "
"(GBP) Japanese Yen, ¥ (JPY) Swiss Franc, Fr (CHF) Australian Dollar, $ (AUD) Canadian Dollar, $ "
"(CAD) Hong Kong Dollar, $ (HKD) Chinese Yuan, ¥ (CNY) Russian Ruble, ₽ (RUB)"
<< endl;
cout << " " << endl;
cout << "European Currencies: Albanian Lek (ALL) Bosnia-Herzegovina Convertible Mark (BAM) Bulgarian Lev "
"(BGN) Belarusian Ruble (BYN) Czech Republic Koruna (CZK) Danish Krone (DKK) Guernsey Pound (GGP) "
"Gibraltar Pound (GIP) Croatian Kuna (HRK) Hungarian Forint (HUF) Icelandic Króna (ISK) Jersey "
"Pound (JEP) Moldovan Leu (MDL) Macedonian Denar (MKD) Manx pound (Isle of Man) (IMP) Norwegian "
"Krone (NOK) Polish Zloty (PLN) Romanian Leu (RON) Serbian Dinar (RSD) Swedish Krona (SEK) "
"Ukrainian Hryvnia (UAH)"
<< endl;
cout << ' ' << endl;
cout << " Middle East Currencies United Arab Emirates Dirham (AED) Bahraini Dinar (BHD) Israeli New Sheqel "
"(ILS) Iraqi Dinar (IQD) Iranian Rial (IRR) Kuwaiti Dinar (KWD) Omani Rial (OMR) Qatari Rial "
"(QAR) Saudi Riyal (SAR) Syrian Pound (SYP) Turkish Lira (TRY) Yemeni Rial (YER) » hide » North "
"American Currencies Bermudan Dollar (BMD) Mexican Peso (MXN) "
<< endl;
cout << "" << endl;
cout << "Central American Currencies: Belize Dollar (BZD) Costa Rican Colón (CRC) Salvadoran Colón (SVC) "
"Guatemalan Quetzal (GTQ) Honduran Lempira (HNL) Nicaraguan Cordoba (NIO) Panamanian Balboa (PAB)"
<< endl;
cout << "" << endl;
cout << "Currencies of the Caribbean: Aruban Florin (AWG) Barbadian Dollar (BBD) Bahamian Dollar (BSD) "
"Cayman Islands Dollar (KYD) Cuban Convertible Peso (CUC) Cuban Peso (CUP) Dominican Peso (DOP) "
"Haitian Gourde (HTG) Jamaican Dollar (JMD) East Caribbean Dollar (XCD) Netherlands Antillean "
"Guilder (ANG) » hide » South American Currencies Argentine Peso (ARS) Bolivian Boliviano (BOB) "
"Brazilian Real (BRL) Chilean Peso (CLP) Chilean Unidad de Fomento (CLF)"
<< endl;
cout << "" << endl;
cout << "South American Currencies: Argentine Peso (ARS) / Bolivian Boliviano (BOB) / Brazilian Real (BRL) "
"/ Chilean Peso (CLP) / Chilean Unidad de Fomento (CLF) / Colombian Peso (COP) / Guyanaese Dollar "
"(GYD) / Peruvian Sol (PEN) / Paraguayan Guarani (PYG) / Surinamese Dollar (SRD) / Trinidad and "
"Tobago Dollar (TTD) / Uruguayan Peso (UYU) / Venezuelan Bolívar Soberano (VES)"
<< endl;
cout << "" << endl;
cout << "African Curriencies: Angolan Kwanza (AOA) / Burundian Franc (BIF) / Botswanan Pula (BWP) / "
"Congolese Franc (CDF) / Cape Verdean Escudo (CVE) / Djiboutian Franc (DJF) / Algerian Dinar (DZD) "
"/ Egyptian Pound (EGP) / Eritrean Nakfa (ERN) / Ethiopian Birr (ETB) / Falkland Islands Pound "
"(FKP) / Gambian Dalasi (GMD) / Ghanaian Cedi (GHS) / Guinean Franc (GNF) / Kenyan Shilling (KES) "
"/ Comorian Franc (KMF) / Lebanese Pound (LBP) / Liberian Dollar (LRD) / Lesotho Loti (LSL) / "
"Libyan Dinar (LYD) / Moroccan Dirham (MAD) / Malagasy Ariary (MGA) / Mauritanian Ouguiya (MRU) / "
"Mauritian Rupee (MUR) / Malawian Kwacha (MWK) / Mozambican Metical (MZN) / Namibian Dollar (NAD) "
"/ Nigerian Naira (NGN) / Rwandan Franc (RWF) / Seychellois Rupee (SCR) / Sudanese Pound (SDG) / "
"South Sudanese Pound (SSP) / Saint Helena Pound (SHP) / Sierra Leonean Leone (SLL) / Somali "
"Shilling (SOS) / São Tomé and Príncipe Dobra (pre-2018) (STD) / São Tomé and Príncipe Dobra (since "
"2018) (STD) / Swazi Lilangeni (SZL) / Tunisian Dinar (TND) / Tanzanian Shilling (TZS) / Ugandan "
"Shilling (UGX) / CFA Franc BEAC (XAF) / CFA Franc BCEAO (XOF) / South African Rand (ZAR) / "
"Zambian Kwacha (ZMW) / Zimbabwean Dollar (ZWL)"
<< endl;
cout << "" << endl;
cout << "Currencies of Australia and Oceania: Fijian Dollar (FJD) / French Polynesia CFP Franc (XPF) / New "
"Zealand Dollar (NZD) / Papua New Guinean Kina (PGK) / Samoan Tala (WST) / Solomon Islands Dollar "
"(SBD) / Tongan Paʻanga (TOP) / Vanuatu Vatu (VUV) "
<< endl;
cout << "" << endl;
cout << "Other Currencies and More: Bitcoin (BTC) / Gold (troy ounce) (XAU) / Silver (troy ounce) (XAG) / "
"Palladium (troy ounce) (XPD) / Platinum (troy ounce) (XPT) / Special Drawing Rights (International "
"Monetary Fund) (XDR) "
<< endl;
}
else if (choice == 0)
{
cout << "Type in the amount of USD you want to convert. NOTE: Other starting currencies coming soon. " << endl;
cin >> choice2;
cout << "Type in the currency you want to convert USD to. " << endl;
cin >> choice3;
}
return 0;
}
Solution 1:[1]
Choose one base currency. Express all other currencies in terms of this base currency. Then you can populate a map of conversion factors and use it:
#include <iostream>
#include <map>
int main()
{
std::map<std::string,int> conversions { {"dollars",100},{"pennies",1}};
// ^ 1 dollar = 100 pennies
std::cout << "amount: ";
double amount = 0;
std::cin >> amount;
std::cout << "from: ";
std::string from;
std::cin >> from;
std::cout << "to : ";
std::string to;
std::cin >> to;
auto ifrom = conversions.find(from);
auto ito = conversions.find(to);
if (ifrom == conversions.end() || ito == conversions.end()) return 0;
double result = amount * ifrom->second / ito->second;
std::cout << result;
}
For example:
amount: 100
from: dollars
to : pennies
10000
std::map<std::string,int> is an associative container. It stores key (std::string)-value (int) pairs. It is sorted, though its just my bad habit of using std::map when an unsorted std::unordered_map would work as well. Its find method can be used to look up an element for a given key. If the key is not found in the map the end iterator is returned. Hence, before dereferencing the returned iterator one needs to check for equality to end(). It the returned iterator is not the end iterator one can use the iterators first and second to get the key and mappend value, respectively.
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|---|
| Solution 1 |
