-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathFinalProject-JamesGarrard.cpp
More file actions
369 lines (339 loc) · 9.89 KB
/
FinalProject-JamesGarrard.cpp
File metadata and controls
369 lines (339 loc) · 9.89 KB
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
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
//C++ Final project
//Country and State facts
//Information sourced from:
//http://www.census.gov/prod/cen2010/cph-2-1.pdf
//http://www.ipl.org/div/stateknow/popchart.html
//https://www.bea.gov/iTable/drilldown.cfm?reqid=70&stepnum=11&AreaTypeKeyGdp=5&GeoFipsGdp=XX&ClassKeyGdp=NAICS&ComponentKey=200&IndustryKey=1&YearGdp=2015Q2&YearGdpBegin=-1&YearGdpEnd=-1&UnitOfMeasureKeyGdp=Levels&RankKeyGdp=1&Drill=1&nRange=5
//Code written by James Garrard
#include <iostream>
#include <fstream>
#include <string>
#include <cmath>
#include <algorithm>
#include <iomanip>
#include <sstream>
using namespace std;
//function prototypes
int findPos(double, double[]);
int findState(string ,string[]);
void posSuffix(int pos);
void addCommas(double num);
//main
int main(int argc, char *argv[]) {
//variables
int l = 1;
int ans = 0;
int position = 0;
int pos=0,highPos=0,lowPos=0;
double find = 0.0;
//inputs
//string inCountry = " ";
string inState = " ";
//data types from setup
char area='Y';
char pop='Y';
char gdp='Y';
char popDens='Y';
char gdpPer='y';
//states
string states[51]={" "};
double areaS[51] = {0.0};
double areaSSorted[51]={0.0};
double popS[51]={0.0};
double popSSorted[51]={0.0};
double gdpS[51]={0.0};
double gdpSSorted[51]={0.0};
double popDensS[51]={0.0};
double popDensSSorted[51]={0.0};
double gdpPerS[51]={0.0};
double gdpPerSSorted[51]={0.0};
//countries
//string countries[195]={" "};
//double areaC[51] = {0.0};
//files
//ifstream inCountries;
ifstream inStates;
//Set precision
cout<<fixed<<setprecision(0);
cout<<"This program lets you find features of states and how they compare to other states.(Including D.C.)"<<endl;
//+++setup+++
cout<<"Would you like to find Area? Y/N"<<endl;
cin>>area;
area = toupper(area);
cout<<"Would you like to find Population? Y/N"<<endl;
cin>>pop;
pop = toupper(pop);
cout<<"Would you like to find GDP? Y/N"<<endl;
cin>>gdp;
gdp = toupper(gdp);
cout<<"Would you like to find Population Density? Y/N"<<endl;
cin>>popDens;
popDens = toupper(popDens);
cout<<"Would you like to find GDP per Capita? Y/N"<<endl;
cin>>gdpPer;
gdpPer = toupper(gdpPer);
cout<<endl;
//+++start of program+++
while(l>0){
cout<<fixed<<setprecision(0);
cout<< "Would you like to learn about:"<<endl<<"(1)States compared to States,"<<endl/*<<"(2)Countries to Countries,"<<endl<< "(3)States to Countries,"<<endl<< "(4)Countries to States?"<<endl*/<<"Enter 0 to quit."<<endl;
cin>>ans;
cout<<endl;
//+++State V State++++
if(ans == 1){
//open file
inStates.open("StateVState.txt");
//read data
for(int x=0;x<51;x++){
//state
getline(inStates,states[x],'#');
//area
inStates>>areaS[x];
inStates.ignore(1);
//population
inStates>>popS[x];
inStates.ignore(1);
//gdp
inStates>>gdpS[x];
inStates.ignore(1);
}
//add ratio based info
for(int x=0;x<51;x++){
//population density
popDensS[x] = popS[x]/areaS[x];
//gdp per capita
gdpPerS[x] = gdpS[x]/popS[x];
}
//add duplicate data to be sorted
for(int i=0;i<51;i++){
areaSSorted[i] = areaS[i];
popSSorted[i] = popS[i];
gdpSSorted[i] = gdpS[i];
popDensSSorted[i] = popDensS[i];
gdpPerSSorted[i] = gdpPerS[i];
}
//sort second arrays
sort (areaSSorted, areaSSorted+51);
sort (popSSorted, popSSorted+51);
sort (gdpSSorted, gdpSSorted+51);
sort (popDensSSorted, popDensSSorted+51);
sort (gdpPerSSorted, gdpPerSSorted+51);
//have user enter state
cout<< "What State would you like to find?"<<endl;
cin.ignore();
getline(cin,inState);
if (inState=="District of Columbia"){
inState = "D.C.";
}
//find state location on array
position = findState(inState, states);
//If the computer doesn't find a state
if (position == -1){
cout<<"Make sure you capitalize the first letter and don't abreviate."<<endl;
}
//+++Display Information to User+++
else{
if (area == 'Y'){
//+++AREA+++
cout<<endl;
//Find the position of the state in the sorted array
find=areaS[position];
pos = findPos(find, areaSSorted);
//find the states higher and lower
highPos = findPos(areaSSorted[pos+1],areaS);
lowPos = findPos(areaSSorted[pos-1],areaS);
//Display
cout<<"Area:"<<endl;
cout<<states[position]<<" is the ";
//put correct suffix
posSuffix(pos);
cout<<" largest state at ";
addCommas(areaS[position]);
cout<<" square kilometers."<<endl;
if (pos <50){
cout<<states[highPos]<<" is larger at ";
addCommas(areaSSorted[pos+1]);
cout<<" square kilometers."<<endl;
}
if(pos >0){
cout<<states[lowPos]<<" is smaller at only ";
addCommas(areaSSorted[pos-1]);
cout<<" square kilometers."<<endl;
}
}
if (pop =='Y'){
//+++Population+++
cout<<endl;
//Find the position of the state in the sorted array
find=popS[position];
pos = findPos(find, popSSorted);
//find the states higher and lower
highPos = findPos(popSSorted[pos+1],popS);
lowPos = findPos(popSSorted[pos-1],popS);
//Display
cout<<"Population: "<<endl;
cout<<states[position]<<" is the ";
//add suffixes
posSuffix(pos);
cout<<" most populated state with a population of ";
addCommas(popS[position]);
cout<<" people."<<endl;
if (pos <50){
cout<<states[highPos]<<" is larger at ";
addCommas(popSSorted[pos+1]);
cout<<" people."<<endl;
}
if(pos >0){
cout<<states[lowPos]<<" is smaller at only ";
addCommas(popSSorted[pos-1]);
cout<<" people."<<endl;
}
}
if(gdp=='Y'){
//+++GDP+++
cout<<endl;
//Find the position of the state in the sorted array
find=gdpS[position];
pos = findPos(find, gdpSSorted);
//find the states higher and lower
highPos = findPos(gdpSSorted[pos+1],gdpS);
lowPos = findPos(gdpSSorted[pos-1],gdpS);
//Display
cout<<"Gross Domestic Product: "<<endl;
cout<<states[position]<<" is the ";
posSuffix(pos);
cout<<" richest state, it makes $";
addCommas(gdpS[position]);
cout<<endl;
if (pos <50){
cout<<states[highPos]<<" makes more at $";
addCommas(gdpSSorted[pos+1]);
cout<<endl;
}
if(pos >0){
cout<<states[lowPos]<<" makes less at only $";
addCommas(gdpSSorted[pos-1]);
cout<<endl;
}
}
if (popDens == 'Y'){
//+++Population Density+++
cout<<endl;
//Find the position of the state in the sorted array
find=popDensS[position];
pos = findPos(find, popDensSSorted);
//find the states higher and lower
highPos = findPos(popDensSSorted[pos+1],popDensS);
lowPos = findPos(popDensSSorted[pos-1],popDensS);
//Display
cout<<"Population Density:"<<endl;
cout<<states[position]<<" is the ";
posSuffix(pos);
cout<<" densest state at ";
addCommas(popDensS[position]);
cout<<" people per square kilometer."<<endl;
if (pos <50){
cout<<states[highPos]<<" is denser at ";
addCommas(popDensSSorted[pos+1]);
cout<<" people per square kilometer."<<endl;
}
if(pos >0){
cout<<states[lowPos]<<" is less dense at only ";
addCommas(popDensSSorted[pos-1]);
cout<<" people per square kilometer."<<endl;
}
if ( gdpPer == 'Y'){
//+++Population Density+++
cout<<endl;
//Find the position of the state in the sorted array
find=gdpPerS[position];
pos = findPos(find, gdpPerSSorted);
//find the states higher and lower
highPos = findPos(gdpPerSSorted[pos+1],gdpPerS);
lowPos = findPos(gdpPerSSorted[pos-1],gdpPerS);
//Display
cout<<"Gross Domestic Product Per Capita:"<<endl;
cout<<states[position]<<" is the ";
posSuffix(pos);
cout<<" richest state at $";
addCommas(gdpPerS[position]);
cout<<" per capita."<<endl;
if (pos <50){
cout<<states[highPos]<<" makes more at $";
addCommas(gdpPerSSorted[pos+1]);
cout<<" per capita."<<endl;
}
if(pos >0){
cout<<states[lowPos]<<" makes less at only $";
addCommas(gdpPerSSorted[pos-1]);
cout<<" per capita."<<endl;
}
}
}
}
//close the file
inStates.close();
cout<<endl;
}
//Country V Country
else if(ans == 2){
cout<<"Sorry, this feature isn't out yet."<<endl;
}
//State V Country
else if(ans == 3){
cout<<"Sorry, this feature isn't out yet."<<endl;
}
//Country V State
else if(ans == 4){
cout<<"Sorry, this feature isn't out yet."<<endl;
}
else if(ans == 0){break;}//exit the while loop and end the program
else if(ans != 0||1||2||3||4){
cout<<"Please enter a valid option"<<endl<<endl;
}
l++;
}
}
//+++functions+++
int findPos(double input, double regions[]){
for(int x=0;x<51;x++){
if(input==regions[x]){
return x;
}
}
return -1;
}
int findState(string inPut, string array[]){
for(int i=0;i<51;i++){
if (array[i] == inPut){
return i;
}
}
return -1;
}
void posSuffix(int pos){
if (((51-pos)%10==1)){//add st when a number ends in 1
cout<<51-pos<<"st";
}
else if ((51-pos)%10==2){//add nd when ending in 2
cout<<51-pos<<"nd";
}
else if ((51-pos)%10==3){//add rd when 3
cout<<51-pos<<"rd";
}
else{//add th to everything else
cout<<51-pos<<"th";
}
}
void addCommas(double num) {
string numWithCommas=" ";// string which will contain the result
ostringstream convert;// stream used for the conversion
convert<<fixed<<setprecision(2)<<num;// insert double num into stream
numWithCommas = convert.str();// set numWithCommas to the contents of the stream
int insertPosition = numWithCommas.length() - 6;//find where the first comma should go
while (insertPosition > 0) {
numWithCommas.insert(insertPosition, ",");
insertPosition-=3;//find where the next comma should go
}
cout<<numWithCommas;
}