currency conversion using switch

Bjarne Stroustrup “Programming Principles and Practice Using C++”
Chapter 4 Try This page 109
Using std_lib_facilities.h by Bjarne Stroustrup.

Code:

// Philipp Siedler
// Bjarne Stroustrup's PP
// Chapter 4 Try This page 109

#include "std_lib_facilities.h"

int main()
{
	const double yenToDollar = 101.10;
	const double euroToDollar = 0.89;
	const double poundToDollar = 0.76;
	const double yuanToDollar = 6.63;
	const double kronarToDollar = 8.44;

	double dollar = 1;
	char currency = 'a';

	cout& lt; < "Please enter an amount of dollar(s) you want to convert,\nfollowed by the desired conversion currency (yen = y, euro = e, pound = p, yuan = yu, kronar = k): \n"; cin& gt; > dollar& gt; > currency;

	switch (currency)
	{
	case 'y':
		if (dollar* yenToDollar& lt; = 1)
		{
			cout& lt; < dollar& lt; < " dollar is " & lt; < dollar* yenToDollar& lt; < " yen";
		}
		else
		{
			cout& lt; < dollar& lt; < " dollars are " & lt; < dollar* yenToDollar& lt; < " yen\n";
		}
		break;
	case 'e':
		if (dollar* euroToDollar& lt; = 1)
		{
			cout& lt; < dollar& lt; < " dollar is " & lt; < dollar* euroToDollar& lt; < " euro\n";
		}
		else
		{
			cout& lt; < dollar& lt; < " dollars are " & lt; < dollar* euroToDollar& lt; < " euros\n";
		}
		break;
	case 'p':
		if (dollar* poundToDollar& lt; = 1)
		{
			cout& lt; < dollar& lt; < " dollar is " & lt; < dollar* poundToDollar& lt; < " pound\n";
		}
		else
		{
			cout& lt; < dollar& lt; < " dollars are " & lt; < dollar* poundToDollar& lt; < " pounds\n";
		}
		break;
	case 'yu':
		if (dollar* yuanToDollar& lt; = 1)
		{
			cout& lt; < dollar& lt; < " dollar is " & lt; < dollar* yuanToDollar& lt; < " yuan\n";
		}
		else
		{
			cout& lt; < dollar& lt; < " dollars are " & lt; < dollar* yuanToDollar& lt; < " yuans\n";
		}
		break;
	case 'k':
		if (dollar* kronarToDollar& lt; = 1)
		{
			cout& lt; < dollar& lt; < " dollar is " & lt; < dollar* kronarToDollar& lt; < " kronar\n";
		}
		else
		{
			cout& lt; < dollar& lt; < " dollars are " & lt; < dollar* kronarToDollar& lt; < " kronars\n";
		}
		break;
	default:
		cout& lt; < "I dont know what " & lt; < currency& lt; < " for an currency is\n";
		break;
	}

	keep_window_open();
}

Output:

Please enter an amount of dollar(s) you want to convert,
followed by the desired conversion currency (yen = y, euro = e, pound = p, yuan = yu, kronar = k):
130 p
130 dollars are 98.8 pounds
Please enter a character to exit

Newsletter Updates

Enter your email address below to subscribe to our newsletter

Leave a Reply

Physical Address

304 North Cardinal St.
Dorchester Center, MA 02124