First Project, Output calculation is very wrong

hello

I've decided to start coding my first project and I've chosen a currency converter. It seems to be working in regards to no errors, but somewhere along the way my calculations are messing up and it's not selecting the proper currency I wish to convert to. It understands the current selected currency and outputs that correctly but returns it in the form of Japanese Yen. I removed my setprecision(); function to see exactly what numbers are being returned and I have a feeling some sort of overflow is occurring. I need to incorporate a loop at some point but I am not too familiar with that yet, so I am waiting to do that after the converter works properly. Any/ all help is greatly appreciated. Thanks.

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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
 
 #include <iostream>
#include <cmath>
#include <string>
#include <cstdlib>
#include <iomanip>
using namespace std;
int main(){

string currencyCode;
double currAmount;
double currencyType;
string wantCurrCode;
double convAmount;
double convType;


double EUR; //European Euros and its conversion rates
double EURtoUSD = EUR * 1.12 ;
double EURtoJPY = EUR * 128.57;
double EURtoGBP = EUR * 0.83;
double EURtoCHF = EUR * 1.04;


double USD; //United States Dollars and its conversion rates
double USDtoEUR = USD * 0.9;
double USDtoJPY = USD * 115.26;
double USDtoGBP = USD * 0.75;
double USDtoCHF = USD * 0.93;

double JPY;  //Japanese Yen and its conversion rates
double JPYtoEUR = JPY * 0.0078;
double JPYtoUSD = JPY * 0.0087;
double JPYtoGBP = JPY * 0.0065;
double JPYtoCHF = JPY * 0.0081;

double GBP;  //British Pound and its conversion rates
double GBPtoEUR = GBP * 1.20;
double GBPtoUSD = GBP * 1.34;
double GBPtoJPY = GBP * 154.28;
double GBPtoCHF = GBP * 1.25;

double CHF;  //Swiss Franc and its conversion rates
double CHFtoEUR = CHF * 0.96;
double CHFtoUSD = CHF * 1.07;
double CHFtoGBP = CHF * 0.80;
double CHFtoJPY = CHF * 128.83;

cout << endl;

// The initial inquiry to determine the current kind of currency
cout << "Please enter your Currencies Code (ISO 4217)." << endl;
cin >> currencyCode;
cout << endl;


// The assignment of the current currency
if (currencyCode == "EUR") {
    currencyType = EUR;
    cout << "The currency you have selected is the European Euro." << endl << endl;
} else if (currencyCode == "USD") {
    currencyType == USD;
    cout << "The currency you have selected is the US Dollar." << endl << endl;
} else if (currencyCode == "JPY") {
    currencyType == JPY;
    cout << "The currency you have selected is the Japanese Yen." << endl << endl;
} else if (currencyCode == "GBP") {
    currencyType == GBP;
    cout << "The currency you have selected is the British Pound." << endl << endl;
} else if (currencyCode == "CHF") {
    currencyType == CHF;
    cout << "The currency you have selected is the Swiss Franc." << endl << endl;
} else {
    cout << "Your currency is not supported. Please try again." << endl << endl;
}; 


// The second inquiry to determine the initial amount of currency possessed
cout << "Please enter your current amount of currency." << endl;
cin >> currAmount; 
cout << endl;
currAmount = currencyType;


// The third inquiry to determine which currency to convert to and calculating it
cout << "Please enter your desired currencies' code." << endl;
cin >> wantCurrCode;
cout << endl;
if (wantCurrCode == "EUR") {
    convType == EUR;
    cout << "You are converting to European Euros." << endl << endl;
} else if (wantCurrCode == "USD") {
    convType == USD;
    cout << "You are converting to US Dollars." << endl << endl;
} else if (wantCurrCode == "JPY") {
    convType == JPY;
    cout << "You are converting to Japanese Yen." << endl << endl;
} else if (wantCurrCode == "GBP") {
    convType == GBP;
    cout << "You are converting to British Pounds." << endl << endl;
} else if (wantCurrCode == "CHF") {
    convType == CHF;
    cout << "You are converting to Swiss Francs." << endl << endl;
} else {
    cout << "Your currency is not supported. Please try again." << endl << endl;
};



//Output statement for Euros to other
if (currencyType == EUR && convType == USD) {
convAmount = EURtoUSD;
cout <<  "€" << currAmount << " is equal to $" << convAmount << "." << endl;
} 
    else if (currencyType == EUR && convType == JPY) {
    convAmount = EURtoJPY;
    cout <<  "€" << currAmount << " is equal to JP¥" << convAmount << "." << endl;
} 
    else if (currencyType == EUR && convType == GBP) {
    convAmount = EURtoGBP;
    cout <<  "€" << currAmount << " is equal to £" << convAmount << "." << endl;
} 
    else if (currencyType == EUR && convType == CHF) {
    convAmount = EURtoCHF;
    cout <<  "€" << currAmount << " is equal to " << convAmount << " francs." << endl;
} 
    else if (currencyType == EUR && convType == EUR) {
    cout <<  "€" << currAmount << " is equal to €" << convAmount << "." << endl;
} 


//Output statement for US Dollars to other
    else if (currencyType == USD && convType == EUR) {
    convAmount = USDtoEUR;
    cout <<  "$" << currAmount << " is equal to €" << convAmount << "." << endl;
} 
    else if (currencyType == USD && convType == JPY) {
    convAmount = USDtoJPY;
    cout <<  "$" << currAmount << " is equal to JP¥" << convAmount << "." << endl;
} 
    else if (currencyType == USD && convType == GBP) {
    convAmount = USDtoGBP;
    cout <<  "$" << currAmount << " is equal to £" << convAmount << "." << endl;
} 
    else if (currencyType == USD && convType == CHF) {
    convAmount = USDtoCHF;
    cout <<  "$" << currAmount << " is equal to " << convAmount << " francs." << endl;
} 
    else if (currencyType == USD && convType == USD) {
    cout <<  "$" << currAmount << " is equal to $" << convAmount << "." << endl;
} 


//Output statement for Japanese Yen to other
    else if (currencyType == JPY && convType == EUR) {
    convAmount = JPYtoEUR;
    cout <<  "JP¥" << currAmount << " is equal to €" << convAmount << "." << endl;
} 
    else if (currencyType == JPY && convType == USD) {
    convAmount = JPYtoUSD;
    cout <<  "JP¥" << currAmount << " is equal to JP¥" << convAmount << "." << endl;
} 
    else if (currencyType == JPY && convType == GBP) {
    convAmount = JPYtoGBP;
    cout <<  "JP¥" << currAmount << " is equal to £" << convAmount << "." << endl;
} 
    else if (currencyType == JPY && convType == CHF) {
    convAmount = JPYtoCHF;
    cout <<  "JP¥" << currAmount << " is equal to " << convAmount << " francs." << endl;
} 
    else if (currencyType == JPY && convType == JPY) {
    cout <<  "JP¥" << currAmount << " is equal to JP¥" << convAmount << "." << endl;
} 


//Output statement for British Pounds to other
    else if (currencyType == GBP && convType == EUR) {
    convAmount = GBPtoEUR;
    cout <<  "£" << currAmount << " is equal to €" << convAmount << "." << endl;
} 
    else if (currencyType == GBP && convType == USD) {
    convAmount = GBPtoUSD;
    cout <<  "£" << currAmount << " is equal to $" << convAmount << "." << endl;
} 
    else if (currencyType == GBP && convType == JPY) {
    convAmount = GBPtoJPY;
    cout <<  "£" << currAmount << " is equal to JP¥" << convAmount << "." << endl;
} 
    else if (currencyType == GBP && convType == CHF) {
    convAmount = GBPtoCHF;
    cout <<  "£" << currAmount << " is equal to " << convAmount << " francs." << endl;
} 
    else if (currencyType == GBP && convType == GBP) {
    cout <<  "£" << currAmount << " is equal to £" << convAmount << "." << endl;
} 
    

//Output statement for Swiss Francs to other
    else if (currencyType == CHF && convType == EUR) {
    convAmount = CHFtoEUR;
    cout <<  currAmount << " francs is equal to €" << convAmount << "." << endl;
} 
    else if (currencyType == CHF && convType == USD) {
    convAmount = CHFtoUSD;
    cout <<  currAmount << " francs is equal to $" << convAmount << "." << endl;
} 
    else if (currencyType == CHF && convType == JPY) {
    convAmount = CHFtoJPY;
    cout <<  currAmount << " francs is equal to JP¥" << convAmount << "." << endl;
} 
    else if (currencyType == CHF && convType == GBP) {
    convAmount = CHFtoGBP;
    cout <<  currAmount << " francs is equal to £" << convAmount << " francs." << endl;
} 
    else if (currencyType == CHF && convType == CHF) {
    cout <<  currAmount << " francs is equal to " << convAmount << " francs." << endl;
} 
    else { cout << "Your desired currency is not supported. Please try again." << endl;
    }



return 0;
};
Last edited on
This is an example of what it returns:

Please enter your Currencies Code (ISO 4217).
GBP

The currency you have selected is the British Pound.

Please enter your current amount of currency.
5

Please enter your desired currencies' code.
CHF

You are converting to Swiss Francs.

JP¥0 is equal to £0.
Variables in programming are not like variables in mathematics. Calculations happen in order, as the program runs.

1
2
double EUR; //European Euros and its conversion rates
double EURtoUSD = EUR * 1.12 ;

This is already junk, because EUR has not been assigned a proper value at this point. Junk in, junk out. EUR has a junk value, so EURtoUSD and any calculations that use EURtoUSD will also be junk.

I would get rid of your 'currencyType' variable and just stick with the strings for now.

something like this:
1
2
const double USDtoEUR = 0.9;
// ... 


1
2
3
4
5
6
7
8
if (currencyCode == "USD" && wantCurrCode == "EUR")
{
    currAmount = currAmount * USDtoEUR;
}
else if (currencyCode == "USD" && wantCurrCode == "...")
{
    // ...
}


PS: I suggest more consistent variable names. You use 'curr' to both mean 'currency' or 'current' depending on the context.

Get that working first. But then, once you do get it working, you can simplify your logic by converting the amount to a particular known currency first, and then converting to the output currency. This will let you have N if statements instead of N^2 if statements. (And you could use a map/dictionary/associative array to further simplify it)
Last edited on
Topic archived. No new replies allowed.