-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
154 lines (143 loc) · 5.14 KB
/
main.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
#include <iostream> // for input/output streams
#include "fstream" // for reading/writing files streams
#include "string" // for dealing with strings
#include "stdc++.h" // for transforming from string to std::uppercase/std::lowercase
using namespace std; // for compatibility with all input/output streams functions
// Declaring the app main arrays
/*
arraySize => the size of the all arrays
displayArraySize => the size of the currencies_from array
displayArraySize => the size of the currencies array
currency_value => the currency_to values
*/
int arraySize, displayArraySize;
string *currencies,*currencies_exchange;
float *currency_value;
// this method for checking if the element value is in the array returned true if the value exists in the array
bool in_array(string value, int size)
{
for (int i = 0; i < size; i++)
{
if (currencies[i] == value)
return true;
}
return false;
}
// this method for returning the currency_to exchange value its returned 0.0 if the value is not in the array
float getCurrencyPrice(string value_from,string value_to,float mony)
{
for (int i = 0; i < displayArraySize; i++)
{
if (currencies_exchange[i] == (value_from+"->"+value_to)){
return mony * currency_value[i];
}else if (currencies_exchange[i] == (value_to+"->"+value_from)){
return (mony / currency_value[i]);
}
}
return 0.0;
}
// this method for reading all currencies from the file.txt and append the values to the arrays in orderd way
/*
Here we can see all arrays associated with the app functionality is initialized with the size and values
ifstream for reading from the file
*/
void convertCurrencies(string filename)
{
ifstream file(filename.c_str());
ifstream file2(filename.c_str());
string line, word, from_currency, to_currency;
float from, to;
arraySize = 1;
// To get the real file rows count to initailize the arrays size
while (getline(file2, word))
{
arraySize = arraySize + 1;
}
currencies = new string[arraySize];
currency_value = new float[arraySize];
currencies_exchange = new string[arraySize];
int i0 = 0, i = 0;
displayArraySize = 0;
while (file >> from >> from_currency >> to >> to_currency)
{
// To make sure the currency values is not repeated
if (!in_array(from_currency, displayArraySize))
{
currencies[displayArraySize] = from_currency;
displayArraySize = displayArraySize + 1;
}
if (!in_array(to_currency, displayArraySize))
{
currencies[displayArraySize] = to_currency;
displayArraySize = displayArraySize + 1;
}
currencies_exchange[i] = from_currency+"->"+to_currency;
currency_value[i] = to;
i = i + 1;
}
}
int main()
{
// this is the main function
/*
from/to variables is referenced to the currency_from/currency_to value the came up from the user
mony variable is referenced to the mony the was entered by the user hime self to make currency exchange with it
result is the returned value as currency exchange process result
*/
string from, to;
float result, mony;
// here we called the convertCurrencies() function to get all the currencies from the file and initialize the arrays
convertCurrencies("ExchageRate.txt");
cout << "Welcome to Currencies Exchanger ^_^" << endl;
cout << "The currencies we have exchange between are:" << endl;
// this is block for displaying the currencies_from names to the user
for (int i = 0; i < displayArraySize; i++)
{
if (i % 2 == 0)
cout << endl;
else
cout << "\t|\t";
cout << i << "- " << currencies[i];
}
cout<<endl;
// x: its point for relunching the user choices if the result is 0.0 its just for make exception if there is any error in the result
x:
cout << "Please enter the currency you want to exchange from:" << endl;
cin >> from;
// this is function is for convert the string to uppercase to facilitate the currency names entering by the user
transform(from.begin(), from.end(), from.begin(), ::toupper);
// here we checked the name of the currency that entered by the user if its not found this block will re-asking the user for the currency name again
while (!in_array(from, displayArraySize))
{
cout << "Please enter the correct currency you want to exchange from:" << endl;
cin >> from;
transform(from.begin(), from.end(), from.begin(), ::toupper);
}
cout <<"Please enter the currency you want to exchange to:" << endl;
cin >> to;
transform(to.begin(), to.end(), to.begin(), ::toupper);
while (!in_array(to, displayArraySize))
{
cout << "Please enter the correct currency you want to exchange to:" << endl;
cin >> to;
transform(to.begin(), to.end(), to.begin(), ::toupper);
}
cout << "Please enter the currency amount you want to exchange:" << endl;
cin >> mony;
// this is line is to return the currency_to value for exchanging
result = getCurrencyPrice(from, to,mony);
if (result != 0.0)
{
cout << "The result of exchanging from ( " << from << " ) currency to ( " << to << " ) was = " << result << endl;
}
else
{
cout << "operation error.\nPlease try again:" << endl;
// this line is responsible for moving the window to the x: point which we descussed above
goto x;
}
cout << endl;
// this is line to make the window waiting the user to close the window and not closing by itself
system("pause");
return 0;
}