I have code here that converts Yen, Pounds, Yuan and Euro to US Dollars that I wrote for Stroustrup's book, PPP 2, and I need to add conversions from Kroner to it, but I see when I Google "kroner" that there's only those Swedish and Norwegian currencies called Krone. Does the book have a typo, or is there still something I can do here?
It's for a "Try this" exercise in Chapter 4 Section 4.4.1.3; here's what it says:
"Rewrite your currency converter program from the previous Try this to use a
switch -statement. Add conversions from yuan and kroner. Which version of
the program is easier to write, understand, and modify? Why?"
I'd written it to convert from Yen, Euro or Pounds to Dollars originally and then I changed it to also do it for Yuan and also changed it to use a switch-statement for the conversion instead of an if-statement, but I'm stuck on the Kroner part.
This is my code that I wrote (I tried to make the changes specified, but of course I also need the conversion rates for the Kroner):
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
|
// Osman Zakir
// 12 / 5 / 2016
// Bjarne Stroustrup: Programming: Principles and Practice Using C++ 2nd Edition
// Chapter 4 Section 4.4.1.1 Try This Exercise
// This program converts from Yen, Euros or Pounds to Dollars
#include "../../std_lib_facilities.h"
double convert(const char unit, const double amount);
bool check_input(const char unit);
int main()
{
cout << "Please enter a money amount followed by currency (y(en), e(uro), p(ound),\n"
<< "or Y(uan) (must be an uppercase Y, to make sure to not clash with\nlowercase y for y(en)):\n";
double amount = 0;
char unit = ' ';
cin >> amount >> unit;
if (!check_input(unit))
{
cout << "Sorry, I don't know a unit called '" << unit << "'\n";
return 1;
}
double dollars = convert(unit, amount);
cout << "Your money converted to US Dollars is $" << dollars << ".\n";
keep_window_open();
}
double convert(const char unit, const double amount)
{
constexpr double yen_to_dollar = 114.239139;
constexpr double euro_to_dollar = 0.927889;
constexpr double pound_to_dollar = 0.785687;
constexpr double yuan_to_dollar = 0.143708;
double dollars = 0;
switch (unit)
{
case 'y':
dollars = amount / yen_to_dollar;
break;
case 'e':
dollars = amount / euro_to_dollar;
break;
case 'p':
dollars = amount / pound_to_dollar;
break;
case 'Y':
dollars = amount * yuan_to_dollar;
break;
}
return dollars;
}
bool check_input(const char unit)
{
if (unit == 'y' || unit == 'e' || unit == 'p' || unit == 'Y')
{
return true;
}
return false;
}
|