Click here to Skip to main content
15,889,931 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
C++
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
	int number1 = 0;
	double total = 0.0;
	char currency = ' ';

	cout << "Enter amount of us dollars: ";
	cin >> number1;

	cout << "What currency would you like to convert it to?" << endl;
	
	cout << "Canada Dollar (CD)" << endl;
	cout << "Eurozone Euro (EE)" << endl;
	cout << "India Rupee (IR)" << endl;
	cout << "Japan Yen (JY)" << endl;
	cout << "Mexico Peso (MP)" << endl;
	cout << "South Africa Rand (SAR)" << endl;
	cout << "United Kingdom Pound (UKP)" << endl;
	
	cout << "Choose currency by the letter(s) in the parentheses: ";
	cin >> currency;

	if  (currency == 'CD')
		total = 1.01615 * number1;
	else if (toupper (currency == 'EE'))
		total = .638490 * number1;
	else if (toupper (currency == 'IR'))
		total = 40.1798 * number1;
	else if (toupper (currency == 'JY'))
		total = 104.390 * number1;
	else if (toupper (currency == 'MP'))
		total = 10.4613 * number1;
	else if (toupper (currency == 'SAR'))
		total = 7.60310 * number1;
	else if (toupper (currency == 'UKP'))	
		total = .504285 * number1;

	cout << "Currency converted: " << total << endl;
	

	system("pause");
	return 0;
}
Posted
Updated 5-Oct-11 17:12pm
v2
Comments
Sergey Alexandrovich Kryukov 6-Oct-11 1:12am    
Did you hard-coded the rates because you think that rates never change or you think there is another reason for it? :-)
--SA

int number1 = 0;
double total = 0.0;

Change to :

float number1;
float total;

Because a ex: 10.23 is float just 10.00 is 10 is also float.

char currency = ' '; is a bad declaration. A char declaration will work only for ONE character, not 2, 3, 5... characters. Change the declarations. A re-design of your code will work if you use 1 character. example : CD is C, EE is E ....

I try your code, and it did not work. The if statement was not executed. I change if (currency=='C') and I put C , it did work. So :

char currency = ' '; Work only for a ONE Character.
But a string of characters, that I did not know how to declare a string of characters , yet.
 
Share this answer
 
change the type of number1 to double, or cast when you multiply
 
Share this answer
 
I change your code here , and I compile with Code::Blocks and tested :

<br />
#include <iostream><br />
#include <iomanip><br />
#include <cstring> // string.h header for the strcmp()<br />
#include <cstdio><br />
<br />
using namespace std;<br />
<br />
int main()<br />
{<br />
      float number1=0; // float here<br />
      float total=0; // float here<br />
     char currency[4]; // change into a array of strings<br />
<br />
     cout << "Enter amount of us dollars: ";<br />
     cin >> number1;<br />
<br />
     cout << "What currency would you like to convert it to?" << endl;<br />
<br />
     cout << "Canada Dollar (CD)" << endl;<br />
     cout << "Eurozone Euro (EE)" << endl;<br />
     cout << "India Rupee (IR)" << endl;<br />
     cout << "Japan Yen (JY)" << endl;<br />
     cout << "Mexico Peso (MP)" << endl;<br />
     cout << "South Africa Rand (SAR)" << endl;<br />
     cout << "United Kingdom Pound (UKP)" << endl;<br />
<br />
     cout << "Choose currency by the letter(s) in the parentheses: ";<br />
     cin >> currency;<br />
     // use str(currency,"currency_code_here")==0<br />
     if   (strcmp(currency,"CD")==0)<br />
          total = 1.01615 * number1;// like this one<br />
     else if (toupper (currency == 'EE'))<br />
          total = .638490 * number1;<br />
     else if (toupper (currency == 'IR'))<br />
          total = 40.1798 * number1;<br />
     else if (toupper (currency == 'JY'))<br />
          total = 104.390 * number1;<br />
     else if (toupper (currency == 'MP'))<br />
          total = 10.4613 * number1;<br />
     else if (toupper (currency == 'SAR'))<br />
          total = 7.60310 * number1;<br />
     else if (toupper (currency == 'UKP'))<br />
          total = .504285 * number1;<br />
     <br />
<br />
     cout << "Currency converted: " << total << endl;<br />
<br />
<br />
    // system("pause");<br />
     return 0;<br />
}<br />
<br />
<br />
</cstdio></cstring></iomanip></iostream>
 
Share this answer
 
Comments
Philippe Mori 6-Oct-11 20:58pm    
Badly formatted and bogus code...

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900