-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcurrency-converter-1.cpp
148 lines (120 loc) · 4.06 KB
/
currency-converter-1.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
// This program is a converter for 3 currencies
#include <iostream>
#include <iomanip>
using namespace std;
// Declare constant variables
const int USD_TO_EUR = 1;
const int EUR_TO_USD = 2;
const int EUR_TO_UAH = 3;
const int UAH_TO_EUR = 4;
const int USD_TO_UAH = 5;
const int UAH_TO_USD = 6;
const int EXIT_CODE = 7;
string from_currency, to_currency;
// create function prototypes
double currencyConverter(int, double);
void initializeConverter();
int main()
{
initializeConverter(); // Call the function to initialize the conversion
return 0;
}
void initializeConverter()
{
// Declare local variables
int operation_code;
double amount_to_convert;
double converted_amount;
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";
cout << "Enter a code from the list above: ";
cin >> operation_code;
// Check if user entered wrong operation code (maybe a string) and exit if so
if (cin.fail()) {
break;
}
// Check if the input is within the range of allowed operations and take amount input
if (operation_code >= USD_TO_EUR && operation_code <= UAH_TO_USD) {
cout << "\nEnter the amount to convert: ";
cin >> amount_to_convert;
// Check if user entered wrong amount (maybe a string) and exit if so
if (cin.fail()) {
break;
}
// Convert input to a double since the function expects amount as double
amount_to_convert = static_cast<double>(amount_to_convert);
// Call the converter function and save the result in a variable
converted_amount = currencyConverter(operation_code, amount_to_convert);
// 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 " << converted_amount << " " << to_currency << "\n\n=============\n\n";
}
else if (operation_code == EXIT_CODE)
{
// End the program if the user inputted the code to end program
break;
}
else {
// Show a message if the user inputted an operation code that is not within the range of available operations
cout << "\nSorry, the requested operation is not yet available. You can request for it by sending an email to us\n\n";
}
} while (operation_code != EXIT_CODE);
}
double currencyConverter(int operation_code, double amount_to_convert)
{
// Define exchange rates for the 3 currencies
double usd_to_eur_rate = 0.88;
double eur_to_uah_rate = 31.93;
double usd_to_uah_rate = 28.13;
// Declare variable to store the converted amount
double converted_amount = 0;
// Switch statement to check requested operation and performs conversion based on that
switch (operation_code)
{
case USD_TO_EUR:
converted_amount = amount_to_convert * usd_to_eur_rate;
from_currency = "USD";
to_currency = "EUR";
break;
case EUR_TO_USD:
converted_amount = amount_to_convert / usd_to_eur_rate;
from_currency = "EUR";
to_currency = "USD";
break;
case EUR_TO_UAH:
converted_amount = amount_to_convert * eur_to_uah_rate;
from_currency = "EUR";
to_currency = "UAH";
break;
case UAH_TO_EUR:
converted_amount = amount_to_convert / eur_to_uah_rate;
from_currency = "UAH";
to_currency = "EUR";
break;
case USD_TO_UAH:
converted_amount = amount_to_convert * usd_to_uah_rate;
from_currency = "USD";
to_currency = "UAH";
break;
case UAH_TO_USD:
converted_amount = amount_to_convert / usd_to_uah_rate;
from_currency = "UAH";
to_currency = "USD";
break;
default:
break;
}
// Return converted amount and exit the function
return converted_amount;
}