-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathCurrency.h
259 lines (213 loc) · 6.74 KB
/
Currency.h
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
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
#include <iostream>
#include<fstream>
#include<iomanip>
using namespace std;
//class Currency
class Currency :public LoginSignup
{
private:
float rate_wrt_USD; //currency's rates w.r.t USD
string Currency_name; //currency's name
string OBV; //currency's obrivations
float ConvertedAmount; //the resultant amount that will we give user after conversion
string CurrencyName[21]; //string array to store currency names and obrivations that would be fetched from file
string CurrencyOBV[21]; //string array to store currency names and obrivations that would be fetched from file
float CurrencyRates[21]; //string array to store currency rates that would be fetched from file
public:
//constructors
Currency();
Currency(string, string, float);
void setConvertedAmount(string&, string&, float&);
//getters
double getRate_wrt_USD();
string getOBV();
string getCurrencyName();
string getConvertedAmount();
//functions
string displayMenuCurrency();
void displayCurrency();
void checkCurrencyName(string& choice);
//destructor
// ~Currency();
};
Currency::Currency()
{
//default constructor
//in this constructor, all the data from currency file
//[name,obv,rate] , will be fetched and stored in arrays
//Reading data from file
ifstream inputfile;
inputfile.open("Currency.csv");
string name, obv;
for (int i = 0; i < 21; i++)
{
getline(inputfile, name, ','); //fetching currencyname
getline(inputfile, obv, ','); //fetching obbrevations
inputfile >> CurrencyRates[i]; //fetching rates
CurrencyName[i] = name;
CurrencyOBV[i] = obv;
}
//closing the file
inputfile.close();
}
//getters
string Currency::getOBV()
{
return OBV;
}
double Currency::getRate_wrt_USD()
{
return rate_wrt_USD;
}
string Currency::getCurrencyName()
{
return Currency_name;
}
void Currency::setConvertedAmount(string& currency1, string& currency2, float& amount)
{
//the most important function...
//in this function, currency will be conerted
//taking input for both currenies name
system("cls");
cout << "Type the currency from which you want to convert.." << endl << endl;
currency1 = displayMenuCurrency();
cout << "*******************************************************************************" << endl;
system("cls");
cout << "Choose the currency to which you want result.." << endl << endl;
currency2 = displayMenuCurrency();
system("cls");
//taking user input for amount
cout << "\n*******************************************************************************" << endl;
cout << "\n\n\tEnter the amount: ";
cin >> amount;
cout << "\n\n*******************************************************************************" << endl;
string currency1_name;
float currency1_rate;
//assigning rate and name of the currency that is given to converted, with
//the hellp of symbol
for (int i = 0; i < 21; i++)
{
if (CurrencyOBV[i] == currency1)
{
currency1_rate = CurrencyRates[i];
currency1_name = CurrencyName[i];
break;
}
}
//assigning rate and name to the currency that is the resultant, with
//the hellp of symbol
for (int i = 0; i < 21; i++)
{
if (CurrencyOBV[i] == currency2)
{
//assigning to private data member
this->rate_wrt_USD = CurrencyRates[i];
this->Currency_name = CurrencyName[i];
this->OBV = currency2;
break;
}
}
//.....formula used for converision....
/*
example....
currency 1=Omani rial
currency2=PKR
amount=40
converting_to_dollar = amount is divided by the rate of PKR against USD [which is suppose 160pkr for a dollar]
// // // // // // = 40/0.0062 [0.0062=1/160]
convertedamount=converting_to_dollar is being multiuplied by omani rial's rate against USD
*/
//now actual opeartion of conversion
float convereting_to_dollar;
//converting the given currency amount to US dollar
//dividing the given amount with the resultant currency rate_wrt_USD
convereting_to_dollar = amount / rate_wrt_USD;
//now multiplyong to the given currency rate_wrt_USD
//converting to required currency
ConvertedAmount = convereting_to_dollar * currency1_rate;
for (int i = 0; i < 100; i += 5)
{
system("cls");
cout << "Loading " << i << " %";
if (i == 95)
system("cls");
}
cout << "\n\n*******************************************************************************" << endl << endl;
cout << setprecision(3)<< "\n\t\tYour " << amount << " " << currency1 << " = "<< setprecision(3) << ConvertedAmount << " in " << OBV << "\n" << endl;
cout<<amount<<"\t"<<currency1<<endl;
cout<<ConvertedAmount<<" "<<OBV<<endl;
cout << "\n*******************************************************************************" << endl;
system("pause");
}
void Currency::checkCurrencyName(string& choice)
{
//this function will validate user input for currency OBV
bool choicecheck = false;
for (int i = 0; i < choice.length(); i++)
{
choice[i] = toupper(choice[i]);
}
//checking if the user have typed correct input or not
while (choice.length() != 3)
{
cout << "Input must be only of 3 characters [Example \"PKR\"]";
cout << "\nyour choice: ";
cin >> choice;
}
int i = 0;
while (choicecheck == false && i < 21)
{
//total currencies are 21
if (CurrencyOBV[i] == choice)
{
//if input matches with any of 21 currencies, loop will be break
choicecheck = true;
break;
}
else
{
choicecheck = false;
}
if (i == 20)
{
//if all currencies have checked, user will be once again asked for input
i = -1;
cout << "Kindly type input for availibe Currencies [Example \"PKR\"]";
cout << "\nyour choice: ";
cin >> choice;
for (int j = 0; j < choice.length(); j++)
{
choice[j] = toupper(choice[j]);
}
}
i++;
}
}
//extra
void Currency::displayCurrency()
{
cout << rate_wrt_USD << " " << "\b" << Currency_name << " " << OBV;
}
string Currency::displayMenuCurrency()
{
//this function will display the list of availible currency and will also take input for currency
string currency;
// system("cls");
cout << "*******************************************************************************" << endl;
cout << "Note: Type the abbrevations..." << endl;
cout << "Example: for Paksitani currency press type PKR" << endl;
cout << "*******************************************************************************" << endl;
bool choicecheck = false;
string choice;
//displaying list of all currency
for (int i = 0; i < 21; i++)
{
cout << CurrencyName[i] << " (";
cout << CurrencyOBV[i] << ")\t\t\t";
}
cout << "\n\nYour choice: ";
cin >> currency;
checkCurrencyName(currency);
system("cls");
return currency;
}