Need help with simple code!

Hello! First week learning c++ and one of the "try this" problems was to convert currencies." It works up until you enter the currency you want to convert to. Thank you in advance!

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
#include "StdAfx.h"
#include "../std_lib_facilities.h"

int main()
{
double yen=1;
double pound=1;
double euro=1;
double dollar=1;
double amount_money;
string start_type_money;
string end_type_money;
cout << "Please enter the amount of money followed by the type of money (yen,\n"; 
cout <<	"pound, euro, dollar) you wish to convert: ";
cin >> amount_money >> start_type_money;
cout << "Please enter the type of money you wish to convert " << start_type_money << " to ";
cin >> end_type_money;

if (start_type_money=="yen" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = "<< amount_money*0.0071 << "pounds\n";
if (start_type_money=="yen" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*0.0087 << "euro\n";
if (start_type_money=="yen" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*0.0113 << "dollar\n";
if (start_type_money=="pound" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*141.1304 << "yen\n";
if (start_type_money=="pound" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*1.2253 << "euro\n";
if (start_type_money=="pound" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*1.6008 << "dollar\n";
if (start_type_money=="euro" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*114.739 << "yen\n";
if (start_type_money=="euro" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = " << amount_money*0.8099 << "pound\n";
if (start_type_money=="euro" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*1.3015 << "dollar\n";
if (start_type_money=="dollar" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*87.825 << "yen\n";
if (start_type_money=="dollar" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = " << amount_money*0.6199 << "pound\n";
if (start_type_money=="dollar" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*0.7624 << "euro\n";
else
	cout << "Inappropriate amount of money/type of money\n";

keep_window_open();
return 0;
}
Last edited on
you cannot link if else statements as you have done:

1
2
3
4
if (//some code)
else if (//some code)
else if(//some code)
else(//some code) 


(You can have as many else if statements as you want)

Now to finish my project due in 2 days that I started today...
Last edited on
On line 15 you have:
cin >> amount_money >> start_type_money;

You can't do that. Instead of two requests, separate your requests into 3 components: Amount of Money, Start Type, End Type.
I changed the code so I have else if statements rather than if statements and now after I enter the first currency, the program closes. Did I do it right?
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
#include "StdAfx.h"
#include "../std_lib_facilities.h"

int main()
{
double yen=1;
double pound=1;
double euro=1;
double dollar=1;
double amount_money;
string start_type_money;
string end_type_money;
cout << "Please enter the amount of money followed by the type of money (yen,\n"; 
cout <<	"pound, euro, dollar) you wish to convert: ";
cin >> amount_money >> start_type_money;
cout << "Please enter the type of money you wish to convert " << start_type_money << " to ";
cin >> end_type_money;

if (start_type_money=="yen" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = "<< amount_money*0.0071 << "pounds\n";
else if (start_type_money=="yen" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*0.0087 << "euro\n";
else if (start_type_money=="yen" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*0.0113 << "dollar\n";
else if (start_type_money=="pound" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*141.1304 << "yen\n";
else if (start_type_money=="pound" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*1.2253 << "euro\n";
else if (start_type_money=="pound" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*1.6008 << "dollar\n";
else if (start_type_money=="euro" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*114.739 << "yen\n";
else if (start_type_money=="euro" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = " << amount_money*0.8099 << "pound\n";
else if (start_type_money=="euro" && end_type_money=="dollar")
	cout << amount_money << start_type_money << " = " << amount_money*1.3015 << "dollar\n";
else if (start_type_money=="dollar" && end_type_money=="yen")
	cout << amount_money << start_type_money << " = " << amount_money*87.825 << "yen\n";
else if (start_type_money=="dollar" && end_type_money=="pound")
	cout << amount_money << start_type_money << " = " << amount_money*0.6199 << "pound\n";
else if (start_type_money=="dollar" && end_type_money=="euro")
	cout << amount_money << start_type_money << " = " << amount_money*0.7624 << "euro\n";
else
	cout << "Inappropriate amount of money/type of money\n";

keep_window_open();
return 0;
}
Last edited on
Your welcome!
Thank you both for helping, though now that problem is fixed, no matter which currencies and amounts I enter, the else statement pops up, rather than the conversion. Been playing with it a decent amount, just can't seem to find the answer.:
1
2
else
	cout << "Inappropriate amount of money/type of money\n";
Last edited on
start_type_money is string. Try changing this to a float. Also, you never ask the user to input the amount they are trying to convert. Usecin >> This will let the user input the amount. Then usecout << to display the conversion. Maybe instead of checking whether the user has entered a yin or pound, ask them what they want to convert at the beginning.
Then you can use a function to convert.

1
2
3
4
float convertYen (int x)
{
     return x * //I dunno the conversion
}


Put this before main() and then use the function. (if they enter 5 yens [for example] then put cout << convertYen(5);

This might be a lot (and not written very well) but its 2.04 am and I am trying to finish a project right now, but my love of c++ overrides my will to finish my project.

Anyway, hoped that helped!
Last edited on
Topic archived. No new replies allowed.