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
|
// Osman Zakir
// 9 / 15 / 2016
// Stroustrup's Programming: Principles and Practice Using C++ 2nd Edition Chapter 4 Section 4
// "Try This" exercise
// Program to convert Yen, Euros and Pounds into Dollars
// Yen to Dollars = amount of Yen to convert * number of Yen in Dollars
// Euro to Dollars = amount of Euro to convert * rate of Euro to Dollars
// Pound to Dollars = amount of Pounds to convert * rate of Pounds to Dollars
#include "std_lib_facilities.h"
double get_money_amount();
char get_currency();
double calculate_converted_amount(char currency, const double yen_in_dollars, const double euro_to_dollars, const double pound_to_dollars, double amt_of_money);
void print_converted_amount(const double converted_amount, const char currency);
int main()
{
constexpr double yen_in_dollars = 0.0098;
constexpr double euro_to_dollars = 1.12;
constexpr double pound_to_dollars = 1.32;
double amt_of_money = get_money_amount();
char currency = get_currency();
double converted_amount = calculate_converted_amount(currency, yen_in_dollars, euro_to_dollars, pound_to_dollars, amt_of_money);
print_converted_amount(converted_amount, currency);
keep_window_open();
cin.ignore();
return 0;
}
double get_money_amount()
{
cout << "Please enter amount of money:\n";
double amt_of_money = 0;
cin >> amt_of_money;
cin.ignore(32767, '\n');
return amt_of_money;
}
char get_currency()
{
cout << "Please enter currency (e for Euro, y for Yen, p for Pounds):\n";
char currency = 0;
cin >> currency;
cin.ignore(32767, '\n');
return currency;
}
double calculate_converted_amount(char currency, const double yen_in_dollars, const double euro_to_dollars, const double pound_to_dollars, double amt_of_money)
{
double converted_amount;
if (currency == 'e')
{
converted_amount = amt_of_money * euro_to_dollars;
}
else if (currency == 'y')
{
converted_amount = amt_of_money * yen_in_dollars;
}
else if (currency == 'p')
{
converted_amount = amt_of_money * pound_to_dollars;
}
else
{
simple_error("Sorry, I don't know that currency");
}
return converted_amount;
}
void print_converted_amount(const double converted_amount, const char currency)
{
cout << "Your money in ";
if (currency == 'e')
{
cout << "Euros";
}
else if (currency == 'p')
{
cout << "Pounds";
}
else if (currency == 'y')
{
cout << "Yen";
}
cout << " is " << converted_amount << " Dollars.\n";
}
|