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
|
// tyler taormina
// cst 116
// lab 7
// number 1
#include <iostream>
#include <fstream>
#include <algorithm>
#include <stdio.h>
#include <iomanip>
#include <string>
using namespace std;
const int MAX = 20;
void RmString(string arr[], int);
void ClearBuffer();
void DisplayMenu(int&);
void ProcessMenuChoice (int&, int&, int&, int&, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]);
void DisplayEditMenu(int&);
void FindIndex(int& index, int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void ProcessEditChoice (int edit_choice, int index, int& check, int record_counter, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]);
void Edit(int index, int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void FindName(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void SortData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void AddPerson(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void PrintData(int, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX]);
void FindString (string first[], string last[], string phone[], string bday[], int);
int ReadData (ifstream & inFile, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX]);
void OutData(ofstream & outFile, string first[], string last[], string phone[], string bday[], int record_counter );
std::string f_name[MAX];
std::string l_name[MAX];
std::string phone[MAX];
std::string bday[MAX];
int main()
{
int record_counter = 0;
int menu_choice;
int flag = 1;
int index_flag;
ifstream inFile;
//open file and read data
inFile.open ( "data.txt");
if ( inFile.is_open ( ) )
{
record_counter = ReadData (inFile, f_name, l_name, phone, bday);
inFile.close ( );
}
else
{
cout << "Trouble Opening File: inFile";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
//User interacts with program here.
SortData(record_counter, f_name, l_name, phone, bday);
while (flag != 0)
{
DisplayMenu(menu_choice);
ProcessMenuChoice(menu_choice, flag, record_counter, index_flag, f_name, l_name, phone, bday);
SortData(record_counter, f_name, l_name, phone, bday);
ClearBuffer();
}
//Writes changes to the data.txt file
ofstream outFile ( "data.txt" );
if ( outFile.is_open ( ) )
{
OutData(outFile, f_name, l_name, phone, bday, record_counter);
outFile.close ( );
}
else
{
cout << "Trouble Opening File: outFile";
cout << "\n\n\t\t ** About to EXIT NOW! ** ";
}
return 0;
}
void SortData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
int i, j, k;
string temp_first;
string temp_last;
string temp_phone;
string temp_bday;
for (i = 0; i < record_counter - 1; i++)
{
transform(last[i].begin(), last[i].end(), last[i].begin(), ::tolower);
transform(first[i].begin(), first[i].end(), first[i].begin(), ::tolower);
for (j = i + 1; j < record_counter; j++)
{
transform(first[j].begin(), first[j].end(), first[j].begin(), ::tolower);
for (k = 0; k < last[i].length(); k++)
{
if (last[i][k] > last[j][k])
{
temp_last = last[j];
last[j] = last[i];
last[i] = temp_last;
temp_first = first[j];
first[j] = first[i];
first[i] = temp_first;
temp_phone = phone[j];
phone[j] = phone[i];
phone[i] = temp_phone;
temp_bday = bday[j];
bday[j] = bday[i];
bday[i] = temp_bday;
k = last[i].length();
}
else if (last[i][k] < last[j][k])
{
k = last[i].length();
}
}
}
}
}
int ReadData ( ifstream & inFile, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
int counter = 0;
inFile >> first[counter] >> last[counter] >> phone[counter] >> bday[counter]; // Priming Read
while ( !inFile.eof ( ) )
{
counter++;
inFile >> first[counter] >> last[counter] >> phone[counter] >> bday[counter];
}
return counter;
}
void FindIndex(int& index, int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
// Check for a substring inside of one of the input strings.
// Prints whether or not the string was found. If found
// prints the index of the list where the string was found
// and what the string was that contained the substring.
string search;
int flag = 0;
int left = 0;
int right = record_counter;
int mid;
cout << "Enter the last name of the person you wish to find: ";
getline(cin, search);
transform(search.begin(), search.end(), search.begin(), ::tolower);
while (left <= right && flag == 0)
{
mid = (left + right) / 2;
if (search == last[mid])
{
cout << "WE FOUND IT!" << endl;
cout << first[mid] << " " << last[mid] << " " << phone[mid] << " " << bday[mid] << endl;
index = mid;
flag = 1;
}
else if(search > last[mid])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
if (flag == 0)
cout << "The last name you entered doesn't exist in our database. Please check spelling and try again." << endl;
}
void FindName(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
// Check for a substring inside of one of the input strings.
// Prints whether or not the string was found. If found
// prints the index of the list where the string was found
// and what the string was that contained the substring.
string search;
int flag = 0;
int left = 0;
int right = record_counter;
int mid;
cout << "Enter the last name of the person you wish to find: ";
getline(cin, search);
transform(search.begin(), search.end(), search.begin(), ::tolower);
while (left <= right && flag == 0)
{
mid = (left + right) / 2;
if (search == last[mid])
{
cout << "WE FOUND IT!" << endl;
cout << first[mid] << " " << last[mid] << " " << phone[mid] << " " << bday[mid] << endl;
flag = 1;
}
else if(search > last[mid])
{
left = mid + 1;
}
else
{
right = mid - 1;
}
}
if (flag == 0)
cout << "The name you entered doesn't exist in our database. Please check spelling and try again." << endl;
}
void PrintData(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
//Displays all strings stored.
int i = 0;
cout << "==================================================================\n";
cout << "This is what we have in our database so far..." << endl;
cout << "==================================================================\n";
while (i < record_counter)
{
cout << i+1 << ") " << first[i] << " " << last[i] << " " << phone[i] << " " << bday[i] << endl;
i++;
}
}
void AddPerson(int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
int i = record_counter - 1;
std::string temp;
cout << "Enter the first name of the person you'd like to add: " << endl;
cin >> temp;
first[i] = temp;
cout << "Enter the last name of the person you'd like to add: " << endl;
cin >> temp;
last[i] = temp;
cout << "Enter the phone number of the person you'd like to add: " << endl;
cin >> temp;
phone[i] = temp;
cout << "Enter the birthday of the person you'd like to add: " << endl;
cin >> temp;
bday[i] = temp;
}
void Edit(int index, int record_counter, string first[MAX], string last[MAX], string phone[MAX], string bday[MAX])
{
int edit_choice;
int check = 1;
while (check != 0)
{
DisplayEditMenu(edit_choice);
ProcessEditChoice(edit_choice, index, check, record_counter, first, last, phone, bday);
}
}
void DisplayEditMenu(int& edit_choice)
{
//Displays the menu of functions for the user to choose from.
edit_choice = 0;
cout << "==================================================================\n";
cout << " MENU" << endl;
cout << "==================================================================\n";
cout << "1) Edit First Name.\n";
cout << "2) Edit Last Name.\n";
cout << "3) Edit Phone Number.\n";
cout << "4) Edit Birth Date.\n";
cout << "Enter: ";
cin >> edit_choice;
if (edit_choice > 4 || edit_choice < 1)
{
cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl;
DisplayMenu(edit_choice);
}
}
void ProcessEditChoice (int edit_choice, int index, int& check, int record_counter, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX])
{
//Takes in user input for menu choice and calls the appropriate function.
string temp;
ClearBuffer();
switch(edit_choice)
{
case 1:
cout << "Enter the updated First name: ";
cin >> temp;
first[index] = temp;
break;
case 2:
cout << "Enter the updated Last name: ";
cin >> temp;
last[index] = temp;
SortData(record_counter, f_name, l_name, phone, bday);
break;
case 3:
cout << "Enter the updated Phone Number: ";
cin >> temp;
phone[index] = temp;
break;
case 4:
cout << "Enter the updated Birthday: ";
cin >> temp;
bday[index] = temp;
break;
default:
break;
}
cout << "Enter 0 to end editing.\n" << endl;
cout << "Enter any other number to continue editing.\n" << endl;
cin >> check;
}
void DisplayMenu(int& menu_choice)
{
//Displays the menu of functions for the user to choose from.
menu_choice = 0;
cout << "==================================================================\n";
cout << " MENU" << endl;
cout << "==================================================================\n";
cout << "1) Find a person's information.\n";
cout << "2) Add a new person to the database.\n";
cout << "3) Edit information for a certain individual.\n";
cout << "4) Display full database.\n";
cout << "5) Exit Program.\n\n";
cout << "Enter: ";
cin >> menu_choice;
if (menu_choice > 5 || menu_choice < 1)
{
cout << "Invalid Entry. Please enter a number from the options list provided.\n\n\n\n" << endl;
DisplayMenu(menu_choice);
}
}
void ProcessMenuChoice (int& menu_choice, int& flag, int& record_counter, int& index, std::string first[MAX], std::string last[MAX], std::string phone[MAX], std::string bday[MAX])
{
//Takes in user input for menu choice and calls the appropriate function.
ClearBuffer();
flag = 0;
switch(menu_choice)
{
case 1:
FindName(record_counter, first, last, phone, bday);
break;
case 2:
record_counter++;
AddPerson(record_counter, first, last, phone, bday);
SortData(record_counter, first, last, phone, bday);
break;
case 3:
FindIndex(index, record_counter, first, last, phone, bday);
Edit(index, record_counter, first, last, phone, bday);
break;
case 4:
PrintData(record_counter, first, last, phone, bday);
break;
case 5:
cout << "Ending program." << endl;
cout << "Are you sure you want to quit?" << endl;
break;
default:
break;
}
cout << "Press 0 to close the program.\n" << endl;
cout << "Press any other number to continue the program.\n" << endl;
cin >> flag;
}
void OutData(ofstream & outFile, string first[], string last[], string phone[], string bday[], int record_counter )
{
int i;
cout << "Writing Changes..." << endl;
for (i = 0; i < record_counter; i++)
{
outFile << first[i] << " " << last[i] << " " << phone[i] << " " << bday[i] << endl;
}
}
void ClearBuffer()
{
// clears buffer after menu choice so as not to interfere with the following user inputs.
char c;
do {
c = getchar();
} while (c != '\n' && c != EOF);
}
|