aboutsummaryrefslogtreecommitdiff
path: root/CST116F2021-Lab9/CST116F2021-Lab9.cpp
blob: 13725069d42c7bad88f6527714be77bb543bdf6c (plain) (blame)
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
//Code by Jordan Harris-Toovy for OIT's CST116-01P lab 9, December 2021

#include <sstream>
#include <iostream>
#include <string>
#include <fstream>
#include <iomanip>
#include <limits>

using namespace std;

#define MV_1 10
#define MV_3 30

//16a - 11.14 Programming Exercises #2:
/*
void bubiSort(int[MV_1], int);
int loadFileData(ifstream&, int[MV_1]);

int main(void)
{
    string path = "C:\\Users\\jorda\\source\\repos\\cst116-lab9-JordanHT-OIT\\CST116F2021-Lab9\\", fileName, ext = ".txt";
    ifstream dataFile;
    int length = 0, data[MV_1];
    bool fileValid = false;

    do
    {
        cout << "Enter name of data file: ";

        getline(cin, fileName);

        dataFile.open(path + fileName + ext);

        fileValid = !dataFile.fail();

        if (!fileValid)
        {
            cout << "Invalid file name" << endl;
        }

    }
    while (!fileValid);

    length = loadFileData(dataFile, data);

    dataFile.close();

    bubiSort(data, length);

    cout << "The smallest number in the file is: " << data[0] << "\nThe largest number in the file is: " << data[length - 1] << endl;
    cout << "The file contents are: " << endl;

    for (int idx = 0; idx < length; idx++)
    {
        cout << data[idx] << " ";
    }
    
    cout << endl;

    return (0);
}

int loadFileData(ifstream& inData, int arr[MV_1])
{
    int entries = 0;

    while (!inData.eof())
    {
        inData >> arr[entries];
        entries++;
    }

    return (--entries);
}

void bubiSort(int arr[MV_1], int len)
{
    int pass = 0, hold = 0;
    while (pass <= len)
    {
        for (int idx = 0; idx < (len - 1); idx++)
        {
            if (arr[idx] > arr[idx + 1])
            {
                hold = arr[idx + 1];
                arr[idx + 1] = arr[idx];
                arr[idx] = hold;
                pass = 0;
            }
            else
            {
                pass++;
            }
        }
    }
}
*/

//16a - 11.14 Programming Exercises #3:
/*
int main(void)
{
    string path = "C:\\Users\\jorda\\source\\repos\\cst116-lab9-JordanHT-OIT\\CST116F2021-Lab9\\", fileName, ext = ".txt", currentLine;
    ifstream dataFile;
    bool fileFailed = false;
    int lineNumber = 0;

    do
    {
        cout << "Enter name of data file: ";

        getline(cin, fileName);

        dataFile.open(path + fileName + ext);

        fileFailed = dataFile.fail();

        if (fileFailed)
        {
            cout << "Invalid file name" << endl;
        }

    } while (fileFailed);

    while (!dataFile.eof())
    {
        getline(dataFile, currentLine);

        cout << 1 + lineNumber++ << ")\t" << currentLine << "\t(" << currentLine.length() << " chars)" << endl;
    }

    dataFile.close();

    return (0);
}
*/

//16a - 11.14 Programming Exercises #4:

//Known issue: The code only indexes by the fist letter of the last names, so within the names of the same fist letter, their order will be as entered.

bool getInt(int&);
int indexFistChar(string);
void bubiSortIndex(int[MV_3], int[MV_3], int);
void swapDataArray(string[4][MV_3], int[MV_3], int&);
void populateMetaData(int[2][MV_3], string[4][MV_3], int);
bool populateDataArray(string, string[4][MV_3], int&);
int findPerson(string[4][MV_3], int);
void displayEntry(string[4][MV_3], int);

int main(void)
{
    int menuChoice = 0, numEntries = 0, metaData[2][MV_3]{ 0 }, select = 0;
    string fileLoc = "C:\\Users\\jorda\\source\\repos\\cst116-lab9-JordanHT-OIT\\CST116F2021-Lab9\\1114_4_datafile.txt";
    string mainData[4][MV_3];

    if (!populateDataArray(fileLoc, mainData, numEntries))
    {
        return (1);
    }

    populateMetaData(metaData, mainData, numEntries); //Make fist map
    bubiSortIndex(metaData[0], metaData[1], numEntries); 

    cout << "Personnel manifest explorer MK1" << endl;

    do
    {
        cout << "Enter desired operation:\n" << "1)\tPersonnel lookup\n" << "2)\tAdd entry\n"
            << "3)\tEdit entry\n" << "4)\tDisplay all records\n" << "5)\tExit program" << endl;


        while (!getInt(menuChoice) || (menuChoice > 5) || (menuChoice < 1)) //Get menu choice from user
        {
            cout << "Invalid input; enter again: ";
        }

        switch (menuChoice)
        {
        case(1): //Find personnel infomation by first name and last name, or by phone number

            select = findPerson(mainData, numEntries);

            cout << endl;

            if (select < 0) //Break out if a match was not found or the user quit
            {
                break;
            }

            displayEntry(mainData, select);

            cout << endl << endl;

            break;

        case(2): //Add a person to the database

            cout << "Enter the first name: ";
            cin >> mainData[0][++numEntries];

            cout << "Enter the last name: ";
            cin >> mainData[1][numEntries];

            cout << "Enter the phone number (format: 012-345-6789): ";
            cin >> mainData[2][numEntries];

            cout << "Enter the date of birth (format: yyyy-mm-dd): ";
            cin >> mainData[3][numEntries];

            cout << endl;

            break;

        case(3): //Edit personnel infomation. Uses same logic as case 1 to find the entry to edit

            cout << "Enter 1 if index is known, or 2 to find a person: ";

            while (!getInt(menuChoice) || (menuChoice > 2) || (menuChoice < 1))
            {
                cout << "Invalid input; enter again: ";
            }

            if (menuChoice == 1)
            {
                cout << "Enter index: ";

                if (!getInt(select))
                {
                    cout << "Invalid index";
                }
            }
            else
            {
                select = findPerson(mainData, numEntries);
            }

            do
            {
                cout << "Select element to change:\n1)\tFirst name\n2)\tLast name\n3)\tPhone number\n4)\tDate of birth\n5)\tExit\n";

                while (!getInt(menuChoice) || (menuChoice > 5) || (menuChoice < 1))
                {
                    cout << "Invalid input; enter again: ";
                }

                switch (menuChoice)
                {
                case(1):

                    cout << "Enter the first name: ";
                    cin >> mainData[0][select];

                    break;

                case(2):

                    cout << "Enter the last name: ";
                    cin >> mainData[1][select];

                    break;

                case(3):

                    cout << "Enter the phone number (format: 012-345-6789): ";
                    cin >> mainData[2][select];

                    break;

                case(4):

                    cout << "Enter the date of birth (format: yyyy-mm-dd): ";
                    cin >> mainData[3][select];

                    break;
                }
            }
            while (menuChoice != 5);

            menuChoice = -1;

            break;

        case(4): //Displays all available information

            cout << endl;

            for (int idx = 0; idx <= numEntries; idx++)
            {
                for (int idy = 0; idy <= numEntries; idy++)
                {
                    if (metaData[0][idy] == idx)
                    {
                        displayEntry(mainData, idy);

                        cout << endl;
                    }
                }
            }

            cout << endl;

            break;
        }

        populateMetaData(metaData, mainData, numEntries); //Regenerate map
        bubiSortIndex(metaData[0], metaData[1], numEntries);

    }
    while (menuChoice != 5);

    return (0);
}

//Returns the ASCII value for the fist char (always as uperace) in the input string
int indexFistChar(string inputString)
{
    inputString[0] = toupper(inputString[0]); //Ensure the fist char of the string is uppercase

    return((int)inputString[0]);
}

//Sorts an input array and creates a map of the changes (swap locations) for len number of entries
void bubiSortIndex(int map[MV_3], int dataArray[MV_3], int len)
{
    int pass = 0, hold = 0;
    while (pass <= len)
    {
        for (int idx = 0; idx < (len); idx++)
        {
            if (dataArray[idx] > dataArray[idx + 1])
            {
                hold = dataArray[idx + 1];
                dataArray[idx + 1] = dataArray[idx];
                dataArray[idx] = hold;

                hold = map[idx + 1];
                map[idx + 1] = map[idx];
                map[idx] = hold;

                pass = 0;
            }
            else
            {
                pass++;
            }
        }
    }
}

//Swaps the dataArray using a map for a specified number of entries (unused)
void swapDataArray(string dataArray[4][MV_3], int meta[MV_3], int &entries)
{
    string swapValue[4];

    for (int idx = 0; idx < entries; idx++)
    {
        if(meta[idx] > 0)
        {
            for (int idy = 0; idy < 4; idy++)
            {
                swapValue[idy] = dataArray[idy][idx];

                dataArray[idy][idx] = dataArray[idy][meta[idx]];

                dataArray[idy][meta[idx]] = swapValue[idy];
            }
        }
    }
}

//Fills a metadata array with relevant data
void populateMetaData(int meta[2][MV_3], string dataAray[4][MV_3], int entries)
{
    for (int idx = 0; idx <= entries; idx++)
    {
        meta[0][idx] = idx;
        meta[1][idx] = indexFistChar(dataAray[1][idx]);
    }
}

//Opens a file at the provied path, the enters its contents into an array. Returns false if there is an I/O error.
bool populateDataArray(string fileLoc, string dataArray[4][MV_3], int &numEntries)
{
    ifstream inputFile;
    bool isFileOpen = false;
    int index = 0;

    inputFile.open(fileLoc); //Open datafile and check if opening was successful. If not, diplay error and exit
    isFileOpen = !inputFile.fail();

    if (!isFileOpen)
    {
        cout << "File read error: Cannot open" << endl;
        return (isFileOpen);
    }
    else
    {
        isFileOpen = true;
    }

    inputFile >> dataArray[0][index] >> dataArray[1][index] >> dataArray[2][index] >> dataArray[3][index]; //Priming read
    numEntries = 1;

    while (!inputFile.eof())
    {
        index++;
        inputFile >> dataArray[0][index] >> dataArray[1][index] >> dataArray[2][index] >> dataArray[3][index];
        numEntries++;
    }

    numEntries--;

    inputFile.close();

    return (isFileOpen);
}

//Gets input from user. Returns false and sets input to 0 if entry is not an int.
bool getInt(int& innum)
{
    bool valid = true;

    cin >> innum;

    if (!cin.good())
    {
        cin.clear(); 
        cin.ignore(numeric_limits<streamsize>::max(), '\n');

        valid = false;
        innum = 0;
    }

    return valid;
}

//Find personnel infomation by first name and last name, or by phone number. Returns array location of match (-1 if no match)
int findPerson(string dataArray[4][MV_3], int entries)
{
    int menuVal = 0;
    string first, last;

    cout << "Chose search method: \n1)\tFirst name and last name\n2)\tPhone number\n3)\tCancel" << endl;

    while (!getInt(menuVal) || (menuVal > 3) || (menuVal < 1))
    {
        cout << "Invalid input; enter again: ";
    }

    switch (menuVal)
    {
    case (1):

        cout << "Enter the first name (case sensitive): ";
        cin >> first;

        cout << "Enter the last name (case sensitive): ";
        cin >> last;

        for (int idx = 0; idx <= entries; idx++)
        {
            if (first == dataArray[0][idx])
            {
                if (last == dataArray[1][idx])
                {
                    cout << "Match found at index " << idx << ":";

                    return (idx);
                }
            }
        }

        cout << "Person not found";

        break;

    case (2):

        cout << "Enter the phone number in the format 012-345-6789: ";
        cin >> first;

        for (int idx = 0; idx <= entries; idx++)
        {
            if (first == dataArray[2][idx])
            {
                cout << "Match found at index " << idx << ":";

                return (idx);
            }
        }

        cout << "Person not found";

        break;
    }

    return (-1);
}

//Displays information about selected person
void displayEntry(string dataArray[4][MV_3], int entry)
{
    cout << left;
    cout << setw(25) << dataArray[0][entry] << setw(25) << dataArray[1][entry] << setw(25) << dataArray[2][entry] << setw(25) << dataArray[3][entry];
}