Wrox Programmer Forums
|
C++ Programming General discussions for the C++ language. For questions specific to Microsoft's Visual C++ variant, see the Visual C++ forum instead.
Welcome to the p2p.wrox.com Forums.

You are currently viewing the C++ Programming section of the Wrox Programmer to Programmer discussions. This is a community of software programmers and website developers including Wrox book authors and readers. New member registration was closed in 2019. New posts were shut off and the site was archived into this static format as of October 1, 2020. If you require technical support for a Wrox book please contact http://hub.wiley.com
  #1 (permalink)  
Old April 25th, 2010, 04:37 AM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default loop and switch use in c++

Hello,

I am trying to develop the curreny exchange program and
unbale to use loop and switch statment in following code.

Please help us how can use the loop and switch statement in
following code to convert one currency to another.

Regards


#include <iostream.h>
#include <conio.h>

main()

{
int e,r,d;
int a,eu;
char convert,convertinto;


cout<<"Wellcome to Exchange of Currency Rates Program C++ " <<"\n" <<"\n";
cout<<"Foreign Cuurency Exchange Rates are following" <<"\n";
cout<<"-------------------------------------------- "<<"\n";
cout<<"1 Euro = 112 Rupees." <<"\n";
cout<<"1 Dollar = 84 Rupees." <<"\n";
cout<<"1 Euro = 1.33 Dollars." <<"\n";
cout<<"1 Dollar = 0.75 Euro " <<"\n";
cout<<"-------------------------------------------- "<<"\n";

cout<<"Please select currencies that you want to exchange." <<"\n";
cout<<"Description :" <<"\n";
cout<<"---------------------- "<<"\n";
cout<<"Enter 'R' for Pakistani Rupees." <<"\n";
cout<<"Enter 'E' for Euro." <<"\n";
cout<<"Enter 'D' for Dollar." <<"\n" <<"\n";

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

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


cout<<"Please enter the amount in Euro :";
cin >>eu;


a = eu * 112;
cout<<eu<<" Euro = "<<a<<" "<<"Rupees"<<"\n"<<"\n";



cout<<"Press any key to Exit....";

getch();

}
Reply With Quote
  #2 (permalink)  
Old April 25th, 2010, 12:51 PM
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
  #3 (permalink)  
Old April 25th, 2010, 11:18 PM
Registered User
 
Join Date: Apr 2010
Posts: 7
Thanks: 0
Thanked 0 Times in 0 Posts
Default loop and switch use in c++

Thanks your very much for your help.

Regards
Reply With Quote





Similar Threads
Thread Thread Starter Forum Replies Last Post
Loop twice, then inside loop select nodes?? JohnBampton XSLT 2 March 9th, 2009 05:21 AM
Switch mamatharocks Biztalk 0 May 22nd, 2008 11:29 PM
if or switch Dink Classic ASP Databases 2 February 4th, 2007 04:00 AM
Switch rajuru Beginning PHP 2 February 9th, 2005 11:24 AM
case switch thtrance BOOK: Beginning PHP4/PHP 5 ISBN: 978-0-7645-4364-7; v5 ISBN: 978-0-7645-5783-5 0 August 26th, 2004 02:36 AM





Powered by vBulletin®
Copyright ©2000 - 2020, Jelsoft Enterprises Ltd.
Copyright (c) 2020 John Wiley & Sons, Inc.