-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency-converter-2.cpp
166 lines (136 loc) · 4.96 KB
/
currency-converter-2.cpp
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
// This program is a converter for 3 currencies
#include <iostream>
#include <iomanip>
using namespace std;
// create function prototypes
bool currencyConverter(int, double, double, double&, double&, double&);
int main()
{
// Declare variables
const int USD_TO_EUR = 1,
EUR_TO_USD = 2,
EUR_TO_UAH = 3,
UAH_TO_EUR = 4,
USD_TO_UAH = 5,
UAH_TO_USD = 6,
EXIT_CODE = 7;
const double USD_TO_EUR_RATE = 0.92,
EUR_TO_USD_RATE = 1.08,
EUR_TO_UAH_RATE = 32.09,
UAH_TO_EUR_RATE = 0.031,
USD_TO_UAH_RATE = 29.66,
UAH_TO_USD_RATE = 0.034;
double usd_reserve = 5000,
eur_reserve = 4000,
uah_reserve = 10000;
int operation_code;
double amount_to_convert;
bool result_check;
string from_currency, to_currency;
double result = 0;
cout << "This program is a converter for 3 currencies. See available operations below;\n\n";
// Start a do ... while loop so the program keeps looping till the user exits
do {
// show available operation options and take input
cout << "List of available operations\n------------\n"
<< USD_TO_EUR << ": USD to EUR\n"
<< EUR_TO_USD << ": EUR to USD\n"
<< EUR_TO_UAH << ": EUR to UAH\n"
<< UAH_TO_EUR << ": UAH to EUR\n"
<< USD_TO_UAH << ": USD to UAH\n"
<< UAH_TO_USD << ": UAH to USD\n"
<< EXIT_CODE << ": End the program\n\n";
// Check if user entered wrong operation code or operation code that is not among the listed options
do {
cout << "Enter a code from the list above: ";
cin >> operation_code;
if (cin.fail() || (operation_code < USD_TO_EUR || operation_code > EXIT_CODE))
{
cin.clear(); cin.ignore();
cout << "Operation code must be an integer between \"" << USD_TO_EUR << "\" and \"" << EXIT_CODE << "\"\n\n";
}
else break;
} while (!cin.fail());
// If the operation code is within the range of conversion codes, take amount input and validate it
if (operation_code >= USD_TO_EUR && operation_code <= UAH_TO_USD) {
do {
cout << "\nEnter the amount to convert: ";
cin >> amount_to_convert;
if (cin.fail() || amount_to_convert < 1)
{
cin.clear(); cin.ignore();
cout << "Kindly enter a valid amount.\n";
}
else break;
} while (!cin.fail());
// Convert input to a double since the function expects amount as double
amount_to_convert = static_cast<double>(amount_to_convert);
// Switch statement to check requested operation and perform conversion based on that
switch (operation_code)
{
case USD_TO_EUR:
result_check = currencyConverter(USD_TO_EUR, amount_to_convert, USD_TO_EUR_RATE, usd_reserve, eur_reserve, result);
from_currency = "USD";
to_currency = "EUR";
break;
case EUR_TO_USD:
result_check = currencyConverter(EUR_TO_USD, amount_to_convert, EUR_TO_USD_RATE, eur_reserve, usd_reserve, result);
from_currency = "EUR";
to_currency = "USD";
break;
case EUR_TO_UAH:
result_check = currencyConverter(EUR_TO_UAH, amount_to_convert, EUR_TO_UAH_RATE, eur_reserve, uah_reserve, result);
from_currency = "EUR";
to_currency = "UAH";
break;
case UAH_TO_EUR:
result_check = currencyConverter(UAH_TO_EUR, amount_to_convert, UAH_TO_EUR_RATE, uah_reserve, eur_reserve, result);
from_currency = "UAH";
to_currency = "EUR";
break;
case USD_TO_UAH:
result_check = currencyConverter(USD_TO_UAH, amount_to_convert, USD_TO_UAH_RATE, usd_reserve, uah_reserve, result);
from_currency = "USD";
to_currency = "UAH";
break;
case UAH_TO_USD:
result_check = currencyConverter(UAH_TO_USD, amount_to_convert, UAH_TO_USD_RATE, uah_reserve, usd_reserve, result);
from_currency = "UAH";
to_currency = "USD";
break;
default:
break;
}
// Check if conversion was successfully processed or not
if (result_check)
{
// Set outputs to have 2 decimal points as money's normal format
cout << fixed << setprecision(2);
// Show final output, specifiying the currency converted from, and the currency converted to
cout << "\n" << amount_to_convert << " " << from_currency << " is " << result << " " << to_currency << "\n\n=============\n\n";
}
else {
cout << "\nThe amount provided is greater than our reserve and cannot be converted.\n\n=============\n\n";
}
}
else {
cout << "Thank you for using our program. See you next time.\n";
break;
}
} while (operation_code != EXIT_CODE);
return 0;
}
bool currencyConverter(int operation_code, double amount, double conversion_rate, double& first_currency_reserve, double& second_currency_reserve, double& result)
{
double converted_amount = 0;
converted_amount = amount * conversion_rate; // Process the conversion
// Check if the money in the reserve is sufficient to process the conversion
if (converted_amount > second_currency_reserve)
{
return false;
}
result = converted_amount;
first_currency_reserve -= amount; // Subtract the amount from the currency reserve
second_currency_reserve += result; // Add the converted amount to the converted reserve
return true;
}