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 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
|
#include <iostream>
#include <iomanip>
#include <string>
#include <cmath>
class Currency {
public:
//Declaring the variables
std::string currencyName;
double conversionRate;
public:
//Constructor with two parameters
Currency( std::string currencyName, double conversionRate );
std::string getName() const;
double convert( double euroAmount ) const;
};
Currency::Currency( std::string pCurrencyName, double pConversionRate)
: currencyName{pCurrencyName}, conversionRate{pConversionRate}
{}
std::string Currency::getName() const { return currencyName; }
double Currency::convert( double euroAmount ) const
{
// return with two-digit precision for fraction part
return std::round(euroAmount * conversionRate * 100) / 100.0;
}
double readCurrencyInput()
{
std::cout << "Input your Euro amount: ";
while( true )
{
std::string line;
double amount;
std::getline( std::cin, line );
// stod (string to double) throws an exception if conversion fails
try {
amount = std::stod( line );
} catch (...) {
std::cout << "You entered a wrong input, try again.\n";
continue; // continues the while loop
}
return amount;
}
}
int readChoice()
{
std::cout << "Make your choice: ";
while( true )
{
std::string line;
int choice;
std::getline( std::cin, line);
try {
choice = std::stoi( line );
} catch (...) {
std::cout << "You entered a wrong input, try again.\n";
continue;
}
if( choice >= 0 && choice <= 7 )
return choice;
else
std::cout << "You entered a wrong number, try again.\n";
}
}
void printMenu()
{
std::cout
<< "########################################################\n"
<< "## Choose the Currency; ##\n"
<< "## ##\n"
<< "## 1 = Us Dollar ##\n"
<< "## 2 = Uk Sterling ##\n"
<< "## 3 = Japanese Yen ##\n"
<< "## 4 = Australian Dollar ##\n"
<< "## 5 = Canadian Dollar ##\n"
<< "## 6 = Swiss Franc ##\n"
<< "## 7 = Chinese Renminbi ##\n"
<< "## 0 = Quit ##\n"
<< "## ##\n"
<< "## ##\n"
<< "########################################################\n";
}
int main()
{
using std::cout;
//Giving values to objects in class Currency
Currency usd("US Dollar", 1.10);
Currency gbp("UK Sterling", 0.84);
Currency jpy("Japanese Yen", 120.26);
Currency aud("Australian Dollar", 1.66);
Currency cad("Canadian Dollar", 1.47);
Currency chf("Swiss Franc", 1.06);
Currency rmb("Chinese Yuan", 7.70);
double euroAmount = readCurrencyInput();
// setting the ouput format
cout << std::fixed << std::setprecision(2);
while( true )
{
printMenu();
int choice = readChoice();
if( choice == 0 ) {
std::cout << "Bye!\n";
return 0;
}
cout << "With " << euroAmount << " Euro, you will get ";
switch( choice )
{
case 1: cout << usd.convert( euroAmount ) << " " << usd.getName(); break;
case 2: cout << gbp.convert( euroAmount ) << " " << gbp.getName(); break;
case 3: cout << jpy.convert( euroAmount ) << " " << jpy.getName(); break;
case 4: cout << aud.convert( euroAmount ) << " " << aud.getName(); break;
case 5: cout << cad.convert( euroAmount ) << " " << cad.getName(); break;
case 6: cout << chf.convert( euroAmount ) << " " << chf.getName(); break;
case 7: cout << rmb.convert( euroAmount ) << " " << rmb.getName(); break;
}
cout << '\n';
}
}
|