program for Converting Yen, Euro And Pounds To Dollars

For my book, I'm writing a program that would convert Yen, Euros and Pounds to Dollars. I checked on Google and found that there's 0.0098 Yen in one Dollar, 1.13 Euro to one Dollar and 1 Pound to 1.32 Dollars. I'm not sure how to use those to convert from those currencies to Dollars, though, so that's what I need help on.

I'm assuming that to convert from Yen to Dollars, I should multiply the amount in Yen by the conversation rate. Is that correct? For the other two, would multiplying do it? Or is there something else I have to do? From what I saw on Google, it seems like converting from Euros to Dollars via multiplication gives me an answer that's off by one or two coins.
A conversion rate is always A → B, which is B = A*rate.
To go backwards you'll need to divide: A = B/rate;

Hope this helps.
Since Euro and Pounds are both greater than Dollars, would it be good to divide to convert to Dollars? Or should I multiply here as well?

And if it's multiplication in either case, then it's like this, right?

Dollars = Yen * 0.0098
Dollars = Euro * 1.13
Dollars = Pound * 1.32

Or would it be better to divide for the Yen to Dollar conversion since the Yen is smaller?
It doesn't make any difference whether larger or smaller. What matters is that the rate is a scalar that satisfies the condition I gave you above.

1 yen == 0.0098 dollar
Conversion rate is yen→dollar == 0.0098

If I have 300 yen, yen*rate = dollars, and 300yen*0.0098 = 2.94dollars.
If I have 3 dollars, dollars/rate = yen, and 3.00dollars/0.0098 = about 306 yen.
Yeah, I'm aware that they're constantly changing and aren't static.

@Duoas: Right, I get it. I'll just multiply them all, then.
I'm guessing I misunderstood your question. You weren't asking about the math, were you?

Floating-point arithmetic should be exact enough to get you the correct number of pennies/whatever the base unit is. Is there a specific Euro→Dollar test case that you can share?
Last edited on
This is the code I have right now.

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";
}


Test cases: 45 Yen to Dollars is $0.441. 45 Euros to Dollars is $50.4. And 45 Pounds to Dollars is $59.4.

Conversion rates used are:
Pounds to Dollars: 1.32
Yen to Dollars: 0.0098
Euro to Dollars: 1.12

You can tell that by looking at the code above, though, of course.
That looks right. Not all conversions will come out to even amounts of money, so 44.1 cents is okay. If you like you can round those down, but I wouldn't bother.

Your code is otherwise pretty nicely structured. There are a few beginner issues I would not do, but there is really nothing wrong with it.

One suggestion: use a table. C++ provides a std::map for that purpose, but anything that associates the two values works (a pair of arrays, an array of pairs or structs, etc).

1
2
string currency_rate_names      [] = { "dollars", "euros", "pounds", "yen" };
double currency_rates_to_dollars[] = { 1.0,       1.12,    1.32,     0.0098 };

1
2
3
4
5
6
7
8
9
10
11
12
struct conversion_rate
{
  string name;
  double rate_to_dollar;
};
conversion_rate conversion_rates_to_dollars[] = 
{
  { "dollars", 1.0    },
  { "euros",   1.12   },
  { "pounds",  1.32   },
  { "yen",     0.0098 }
};


Here's my version:
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
#include <cctype>
#include <ciso646>
#include <iostream>
#include <map>
#include <string>

std::map <std::string, double> to_dollar_rates
{
  { "Dollars", 1.0000 },
  { "Euros",   1.1200 },
  { "Pesos",   0.0520 },
  { "Pounds",  1.3200 },
  { "Yen",     0.0098 }
};

std::string totitle( const std::string& s )
{
  std::string r( s );
  for (char& c : r)   c = tolower( c );
  for (char& c : r) { c = toupper( c ); break; }
  return r;
}

std::string select_currency( const std::string& prompt )
{
  std::string currency_name;
  
  std::cout << prompt << "\n";
  for (auto rate_info : to_dollar_rates)
    std::cout << "  " << rate_info.first << "\n";
  std::cout << ": ";
  
  std::getline( std::cin, currency_name );
  currency_name = totitle( currency_name );
  if (!to_dollar_rates.count( currency_name ))
    throw "Not a valid currency";
  
  std::cout << "\n";
  return currency_name;
}

int main()
{
  try
  {
    std::string from_currency = select_currency( "Select a FROM currency:" );
    std::string to_currency   = select_currency( "Select a TO currency:" );
    
    double from_amount;
    std::cout << "Enter an amount to convert: ";
    std::cin >> from_amount;

    double dollars   = from_amount * to_dollar_rates[ from_currency ];
    double to_amount = dollars     / to_dollar_rates[ to_currency ];
    
    std::cout << from_amount << " " << from_currency << " is " 
              << to_amount   << " " << to_currency   << ".\n";
  }
  catch (const char* s)
  {
    std::cout << s << "\n";
    return 1;
  }
}
Select a FROM currency:
  Dollars
  Euros
  Pesos
  Pounds
  Yen
: yen

Select a TO currency:
  Dollars
  Euros
  Pesos
  Pounds
  Yen
: dollars

Enter an amount to convert: 5000
5000 Yen is 49 Dollars.

Hope this helps.
I haven't learned about maps yet in the book. It mentioned it, but it'll teach about them in a later chapter. But yeah, thanks.
@Duoas
How to make your code and your output look like that? I am curious.
use --- at the end of your code and paste the output
http://www.cplusplus.com/articles/z13hAqkS/
Topic archived. No new replies allowed.