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
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
|
/////////////////////////////////////////////
// CODE FOR PROBLEM #2 ON PAGE 337 IS BELOW
/////////////////////////////////////////////
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
using namespace std;
const int MAX_INTEGERS = 10;
void readData(string&, ifstream& inFile, int[]); //takes in user input filename, gets integers from file and puts them into an array
void sort(int[], int[]); //sorts the integers into another array (ascending order)
void displayInfo(int[]); //displays the smallest and greatest integers and then displays the list of sorted integers (ascending order)
int main()
{
string fileName;
int originalList[MAX_INTEGERS];
int sortedList[MAX_INTEGERS];
ifstream inFile;
cout << "\n\t\t============================================";
cout << "\n\t\t Basic integer sorting from file";
cout << "\n\t\t (with smallest & greatest integer found)";
cout << "\n\t\t============================================";
readData(fileName, inFile, originalList);
sort(originalList, sortedList);
displayInfo(sortedList);
return 0;
}
void readData(string& fileName, ifstream& inFile, int originalList[]) //takes in user input filename, gets integers from file and puts them into an array
{
cout << "\n\n\tInput file name (including extension): ";
getline(cin, fileName);
string filePath = "C:\\Users\\eclip\\source\\repos\\cst116-lab9-JosephTenEyck\\CST116F2021-Lab9\\" + fileName;
inFile.open(filePath);
if (inFile.is_open())
{
int i = 0;
inFile >> originalList[i]; //priming read
while (!inFile.eof()) //reads rest of file into array
{
i++;
inFile >> originalList[i];
}
}
else
{
cout << "\n\nERROR: Unable to open file with name '" << fileName << "'" << endl;
exit(1);
}
}
void sort(int originalList[], int sortedList[]) //sorts the integers into another array (ascending order)
{
for (int i = 0; i < MAX_INTEGERS; i++) //makes a copy of array in case the original list order is needed for something
{
sortedList[i] = originalList[i];
}
//actual working bubble sorting algorithm
for (int i = 0; i < MAX_INTEGERS; i++)
{
for (int j = 0; j < MAX_INTEGERS - i - 1; j++)
{
if (sortedList[j] > sortedList[j + 1])
{
int temp = sortedList[j];
sortedList[j] = sortedList[j + 1];
sortedList[j + 1] = temp;
}
}
}
////my sorting algorithm that does not work
//int tempList[MAX_INTEGERS];
//for (int i = 0; i < MAX_INTEGERS - 1; i++) //makes a copy of originalList to be used for sorting algorithm
//{
// tempList[i] = originalList[i];
//}
//
//for (int j = 0; j < MAX_INTEGERS - 1; j++) //each index of sortedList
//{
// if ((sizeof(tempList) / sizeof(tempList[0])) > 1)
// {
// int hold = tempList[0]; //value of current smallest found integer
// int indexH = 0; //index of current smallest found integer
// for (int i = 0; i < (sizeof(tempList) / sizeof(tempList[0])) - 1; i++) //finds smallest number in the current version of tempList
// {
// if (hold < tempList[i + 1])
// {
// hold = hold;
// }
// else if (hold == tempList[i + 1])
// {
// hold = tempList[i + 1];
// indexH++;
// }
// else
// {
// hold = tempList[i + 1];
// indexH++;
// }
// }
// sortedList[j] = hold; //writes currently found smallest integer to sortedList
// for (int k = 0; k < (sizeof(tempList) / sizeof(tempList[0])) - indexH + 1; k++) //removes current smallest found integer from tempList and compresses
// {
// tempList[indexH + k] = tempList[indexH + k + 1];
// }
// }
// else if ((sizeof(tempList) / sizeof(tempList[0])) == 1)
// {
// sortedList[MAX_INTEGERS - 1] = tempList[0];
// }
//}
}
void displayInfo(int sortedList[]) //displays the smallest and greatest integers and then displays the list of sorted integers (ascending order)
{
int smallest = sortedList[0];
int largest = sortedList[MAX_INTEGERS - 1];
cout << "\n\n\tThe smallest number is: " << smallest;
cout << "\n\tThe largest number is: " << largest;
cout << "\n\nThe numbers in order from smallest to largest are :";
cout << "\n\n";
for (int i = 0; i < MAX_INTEGERS; i++)
{
cout << sortedList[i] << ", ";
}
cout << "\n" << endl;
}
/////////////////////////////////////////////
// CODE FOR PROBLEM #3 ON PAGE 337 IS BELOW
/////////////////////////////////////////////
//#include <iostream>
//#include <iomanip>
//#include <fstream>
//#include <string>
//#include <cstdio>
//#include <cstring>
//
//using namespace std;
//
//const int MAX_CHARACTERS = 1000;
//
//
//string readData(ifstream& inFile); //takes in user input filename, reads file, returns string
//void displayText(char[]); // displays text along with line numbers and character counts
//
//int main()
//{
// ifstream inFile;
//
// string text;
//
// cout << "\n\t\t============================================";
// cout << "\n\t\t Basic text file reader";
// cout << "\n\t\t (with line counter and character counter)";
// cout << "\n\t\t=============================================";
//
// text = readData(inFile);
//
// char textArray[MAX_CHARACTERS]{char(0)};
//
// for (int i = 0; i < text.size() - 1; i++) //writes string to char array
// {
// textArray[i] = text[i];
// }
//
// displayText(textArray);
//
// return 0;
//}
//
//string readData(ifstream& inFile) //takes in user input filename, reads file, returns string
//{
// string fileName;
// string text;
//
// cout << "\n\n\tInput file name (including extension): ";
// getline(cin, fileName);
//
// string filePath = "C:\\Users\\eclip\\source\\repos\\cst116-lab9-JosephTenEyck\\CST116F2021-Lab9\\" + fileName;
//
// inFile.open(filePath);
//
// if (inFile.is_open())
// {
// while (!inFile.eof())
// {
// getline(inFile, text, char(0)); //read from inFile, write to "text", until NUL character found
// }
// }
// else
// {
// cout << "\n\nERROR: Unable to open file with name '" << fileName << "'" << endl;
//
// exit(1);
// }
// return text;
//}
//
//void displayText(char textArray[]) // displays text along with line numbers and character counts
//{
// int lineNumber = 2;
// int charCount = 0;
//
// cout << "\n1 ";
//
// for (int i = 0; i < MAX_CHARACTERS - 1; i++)
// {
// if (textArray[i] == '\n')
// {
// cout << " [" << charCount << "]" << endl;
// cout << textArray[i] << lineNumber << " ";
//
// lineNumber++;
// charCount = 0;
// }
// else if (textArray[i] == char(0))
// {
// cout << " [" << charCount << "]" << endl;
// break;
// }
// else
// {
// cout << textArray[i];
//
// charCount++;
// }
// }
//}
/////////////////////////////////////////////
// CODE FOR PROBLEM #3 ON PAGE 337 IS BELOW
/////////////////////////////////////////////
//#include <iostream>
//#include <iomanip>
//#include <fstream>
//#include <string>
//#include <cstdio>
//#include <cstring>
//
//using namespace std;
//
//const int MAX_RECORDS = 10;
//
//int getData(ifstream& inFile, string[], string[], string[], string[]); //reads datafile into arrays
//void displayMenu(int&); //displays menu, takes in user menu choice
//void processMenuChoice(ofstream& outFile, int, int&, string[], string[], string[], string[]); //processes user menu choice
//void findInfo(int, string[], string[], string[], string[]); //finds a person's info
//void addInfo(int&, string[], string[], string[], string[]); //allows user to add a person to the database
//void editInfo(int, string[], string[], string[], string[]); //allows user to edit a person's info
//void displayRecords(int, string[], string[], string[], string[]); //displays all records
//void sort(int, string[], string[], string[], string[]); //sorts records by last name
//void fileWrite(ofstream& outFile, int, string[], string[], string[], string[]); //writes the arrays to the output file
//
//int main()
//{
// ifstream inFile;
// ofstream outFile;
//
// int menuChoice = 1;
// int count = 0;
//
// string firstNames[MAX_RECORDS];
// string lastNames[MAX_RECORDS];
// string phoneNumbers[MAX_RECORDS];
// string birthdays[MAX_RECORDS];
//
//
// cout << "\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~";
// cout << "\n\t\t Basic database program";
// cout << "\n\t\t~~~~~~~~~~~~~~~~~~~~~~~~~~" << endl;
//
// count = getData(inFile, firstNames, lastNames, phoneNumbers, birthdays) + 1;
//
// do
// {
// displayMenu(menuChoice);
// processMenuChoice(outFile, menuChoice, count, firstNames, lastNames, phoneNumbers, birthdays);
//
// } while (menuChoice > 0 && menuChoice < 6);
//
// return 0;
//}
//
//
//int getData(ifstream& inFile, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[])
//{
// inFile.open("C:\\Users\\eclip\\source\\repos\\cst116-lab9-JosephTenEyck\\CST116F2021-Lab9\\Records.txt");
//
// int recordCount = 0;
//
// if (inFile.is_open())
// {
// inFile >> firstNames[recordCount] >> lastNames[recordCount] >> phoneNumbers[recordCount] >> birthdays[recordCount]; //priming read
//
// while (!inFile.eof()) //reads rest of file into array
// {
// recordCount++;
//
// inFile >> firstNames[recordCount] >> lastNames[recordCount] >> phoneNumbers[recordCount] >> birthdays[recordCount];
// }
// inFile.close();
// }
// else
// {
// cout << "\n\nERROR: Unable to open file" << endl;
//
// exit(1);
// }
//
// return recordCount;
//}
//
//void displayMenu(int& menuChoice) //displays menu
//{
// do
// {
// cout << "\n\t\t==============================";
// cout << "\n\t\t What would you like to do?";
// cout << "\n\t\t==============================" << endl;
//
// cout << "\n\n\t1. Find person's info";
// cout << "\n\t2. Add person's info";
// cout << "\n\t3. Edit a person's info";
// cout << "\n\t4. Display all records";
// cout << "\n\t5. Exit";
//
// cout << "\n\n\t\tMenu choice: ";
// cin >> menuChoice;
//
// if (menuChoice < 1 || menuChoice > 5)
// {
// cout << "\nERROR: Invalid menu choice";
// }
// } while (menuChoice < 1 || menuChoice > 5);
//}
//
//void processMenuChoice(ofstream& outFile, int menuChoice, int& count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[])
//{
// switch (menuChoice)
// {
// case 1:
// {
// findInfo(count, firstNames, lastNames, phoneNumbers, birthdays);
// break;
// }
// case 2:
// {
// addInfo(count, firstNames, lastNames, phoneNumbers, birthdays);
// break;
// }
// case 3:
// {
// editInfo(count, firstNames, lastNames, phoneNumbers, birthdays);
// break;
// }
// case 4:
// {
// sort(count, firstNames, lastNames, phoneNumbers, birthdays);
// displayRecords(count, firstNames, lastNames, phoneNumbers, birthdays);
// break;
// }
// case 5:
// {
// fileWrite(outFile, count, firstNames, lastNames, phoneNumbers, birthdays);
//
// cout << "\n\t~~ ~~";
// cout << "\n\t Output file successfully written!";
// cout << "\n\t~~ ~~";
//
// cout << "\n\n\t\t~ Goodbye! ~" << endl;
//
// exit(0);
// break;
// }
// default:
// {
// cout << "\nERROR: Impossible menu value";
// }
// }
//}
//
//void findInfo(int count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //finds and displays a person's info
//{
// string searchFirst;
// string searchLast;
// int i = 0;
//
// cout << "\n\tEnter the person's first name (case sensitive): ";
// cin >> searchFirst;
//
// cout << "\n\tEnter the person's last name (case sensitive): ";
// cin >> searchLast;
//
// do
// {
// if (searchFirst == firstNames[i] && searchLast == lastNames[i])
// {
// cout << "\n~~~~";
// cout << "\n" << "First name: " << firstNames[i];
// cout << "\n" << "Last name: " << lastNames[i];
// cout << "\n" << "Phone number: " << phoneNumbers[i];
// cout << "\n" << "Birthday: " << birthdays[i];
// cout << "\n~~~~" << endl;
//
// break;
// }
//
// i++;
//
// } while (i < count);
//
// if (i == count)
// {
// cout << "\n\tPerson not found." << endl;
// }
//}
//
//void addInfo(int& count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //allows user to add a person to the database
//{
// cout << "\n\tEnter first name (no spaces): ";
// cin >> firstNames[count];
//
// cout << "\n\tEnter last name (no spaces): ";
// cin >> lastNames[count];
//
// cout << "\n\tEnter phone number (ex: 555-123-4567): ";
// cin >> phoneNumbers[count];
//
// cout << "\n\tEnter birthday mm/dd/yyyy (ex: 1/26/1996): ";
// cin >> birthdays[count];
//
// count++;
//
// sort(count, firstNames, lastNames, phoneNumbers, birthdays);
//
// cout << "\n\t~~ ~~";
// cout << "\n\t Data entered successfully!" << endl;
// cout << "\n\t~~ ~~";
//}
//
//void editInfo(int count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //allows user to edit a person's info
//{
// string searchFirst;
// string searchLast;
// int i = 0;
// int editLocation = 0;
// int menu2 = 0;
//
// cout << "\n\tEnter the peron's first name (case sensitive): ";
// cin >> searchFirst;
//
// cout << "\n\tEnter the peron's last name (case sensitive): ";
// cin >> searchLast;
//
// do
// {
// if (searchFirst == firstNames[i] && searchLast == lastNames[i]) //finds index of record to edit
// {
// editLocation = i;
// }
//
// i++;
//
// } while (i < count - 1);
//
// if (i == count - 1)
// {
// cout << "\n\tPerson not found." << endl;
// }
//
// do
// {
// cout << "\n\t\t---------------------------------------";
// cout << "\n\t\t What info would you like to change?";
// cout << "\n\t\t---------------------------------------";
//
// cout << "\n\n\t1. First name";
// cout << "\n\t2. Last name";
// cout << "\n\t3. Phone number";
// cout << "\n\t4. Birthday";
//
// cout << "\n\n\t\tMenu choice: ";
// cin >> menu2;
//
// if (menu2 < 1 || menu2 > 4)
// {
// cout << "\nERROR: Invalid menu choice";
// }
// } while (menu2 < 1 || menu2 > 4);
//
// switch (menu2)
// {
// case 1:
// {
// cout << "\n\tEnter new first name (no spaces): ";
// cin >> firstNames[editLocation];
//
// cout << "\n\tChange successful!" << endl;
//
// break;
// }
// case 2:
// {
// cout << "\n\tEnter new last name (no spaces): ";
// cin >> lastNames[editLocation];
//
// sort(count, firstNames, lastNames, phoneNumbers, birthdays);
//
// cout << "\n\tChange successful!" << endl;
//
// break;
// }
// case 3:
// {
// cout << "\n\tEnter new phone number (ex: 555-123-4567): ";
// cin >> phoneNumbers[editLocation];
//
// cout << "\n\tChange successful!" << endl;
//
// break;
// }
// case 4:
// {
// cout << "\n\tEnter new birthday dd/mm/yyyy (ex: 1/26/1996): ";
// cin >> birthdays[editLocation];
//
// cout << "\n\tChange successful!" << endl;
//
// break;
// }
// default:
// {
// cout << "\nERROR: Impossible menu2 value." << endl;
// }
// }
//}
//
//void displayRecords(int count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //displays all records
//{
// cout << "\n\nName (last, first) " << "Phone Number " << "Birthday";
// cout << "\n------------------------------------------------------------------------------------\n";
//
// for (int i = 0; i < count; i++)
// {
// cout << left << setw(30) << lastNames[i] + ", " + firstNames[i]
// << left << setw(18) << phoneNumbers[i] << left << setw(12) << birthdays[i] << "\n";
// }
// cout << endl;
//}
//
//void sort(int count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //sorts records by last name
//{
// for (int i = 0; i < count; i++) //bubble sort algorithm (efficient)
// {
// for (int j = 0; j < count - i - 1; j++)
// {
// if (lastNames[j] > lastNames[j + 1])
// {
// string temp1 = lastNames[j];
// string temp2 = firstNames[j];
// string temp3 = phoneNumbers[j];
// string temp4 = birthdays[j];
//
// lastNames[j] = lastNames[j + 1];
// lastNames[j + 1] = temp1;
//
// firstNames[j] = firstNames[j + 1];
// firstNames[j + 1] = temp2;
//
// phoneNumbers[j] = phoneNumbers[j + 1];
// phoneNumbers[j + 1] = temp3;
//
// birthdays[j] = birthdays[j + 1];
// birthdays[j + 1] = temp4;
// }
// }
// }
//}
//
//void fileWrite(ofstream& outFile, int count, string firstNames[], string lastNames[], string phoneNumbers[], string birthdays[]) //writes the arrays to the output file
//{
// outFile.open("C:\\Users\\eclip\\source\\repos\\cst116-lab9-JosephTenEyck\\CST116F2021-Lab9\\RecordsOutput.txt");
//
// if (outFile.is_open())
// {
// for (int i = 0; i < count; i++)
// {
// outFile << firstNames[i] << " " << lastNames[i] << " "
// << phoneNumbers[i] << " " << birthdays[i] << "\n";
// }
// outFile.close();
// }
//}
|