View Single Post
  #2 (permalink)  
Old April 25th, 2010, 12:51 PM
PeterPeiGuo PeterPeiGuo is offline
Friend of Wrox
 
Join Date: Dec 2008
Posts: 238
Thanks: 2
Thanked 20 Times in 19 Posts
Default

Store exchange rate in an array, that makes the code neat: (The following is good enough as a simple example. In real life you obviously need to validate the input etc.)
Code:
#include <iostream.h>
#include <conio.h>

int main()
{
	enum Currency {Rupee, Euro, Dollar};
	float rate[3][3];
	rate[Rupee][Rupee] = 1.;
	rate[Rupee][Euro] = 2.;
	rate[Rupee][Dollar] = 4.;
	rate[Euro][Rupee] = 0.5;
	rate[Euro][Euro] = 1.;
	rate[Euro][Dollar] = 2.;
	rate[Dollar][Rupee] = 0.25;
	rate[Dollar][Euro] = 0.5;
	rate[Dollar][Dollar] = 1.;

	int from, to;
	float amount;
	char quit;

	while (1) {
		cout<<"0 - Rupee\n";
		cout<<"1 - Euro\n";
		cout<<"2 - Dollar\n"; 

		cout<<"Please select the correny that you want to convert:";
		cin>>from;

		cout<<"Please enter the amount:";
		cin >>amount;

		cout<<"Please select the correny that you want to convert into:";
		cin>>to;

		cout << "Current exchange rate is: " << rate[from][to] << "\n";
		cout << "Converted amount: " << amount * rate[from][to] << "\n";
		
		cout << "Quit? (y/n) ";
		cin >> quit;
		if (quit == 'y') return 0;
	} 
}
Reply With Quote