blob: 49cb75558ffca39d96f4af1382a45d8d59dab6b5 (
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
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
|
Lab 9 CST116 Benjamin Schroeder
///////////////////////////////////////////////////////////////////////////////////////////////////////////////
//// 11.14 Programming Exercises pp 337 - 338 #2 //////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
char fileName[200];
int inputIntegers[10]{};
ifstream fin;
int main()
{
cout << "Enter the the name of the file you want to open: ";
cin >> fileName;
fin.open(fileName);
while (fin && !fin.eof())
{
for (int i = 0; i < 10; i++)
fin >> inputIntegers[i];
}
int j = 0, k = 0, temp = 0;
for (j = 0; j < 10; j++)
{
for (k = 0; k < 10 - 1; k++)
{
if (inputIntegers[k] > inputIntegers[k + 1])
{
temp = inputIntegers[k];
inputIntegers[k] = inputIntegers[k + 1];
inputIntegers[k + 1] = temp;
}
}
}
cout << "The integers in this file range from " << inputIntegers[0] << " to " << inputIntegers[9]<< "\n";
cout << "\nSorted list of integers from the file:\n";
for (int l = 0; l < 10; l++)
{
cout << inputIntegers[l]<< "\n";
}
}
// ///// Run /////
Enter the the name of the file you want to open: integers.txt
The integers in this file range from 23 to 2021
Sorted list of integers from the file:
23
33
44
120
234
340
501
530
567
2021
C:\Users\Lenovo\source\repos\Lab9_Schroeder\Debug\Lab9_Schroeder.exe (process 13476) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
// /// 11.14 Programming Exercises pp 337 - 338 #3 /////////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
char fileName[200];
int inputIntegers[10]{};
string lines[1000];
int num_records = 0;
ifstream fin;
int main()
{
cout << "Enter the the name of the file you want to open: ";
cin >> fileName;
fin.open(fileName);
while (fin && !fin.eof())
{
getline(fin, lines[num_records]);
cout << num_records+1 << ". " << lines[num_records] << " (" << lines[num_records].length() << " characters)\n";
num_records++;
}
}
// /////////////// ////////// RUN //////////////////////
Enter the the name of the file you want to open: linesOfText.txt
1. The C++ getline() is a standard library function that is used to read a string or a line from an input stream. (110 characters)
2. It is a part of the <string> header. (36 characters)
3. The getline() function extracts characters from the input stream and appends it to the string object until the delimiting character is encountered. (147 characters)
4. While doing so the previously stored value in the string object str will be replaced by the input string if any. (112 characters)
5. The getline() function can be represented in two ways: (54 characters)
C:\Users\Lenovo\source\repos\Lab9_Schroeder\Debug\Lab9_Schroeder.exe (process 3156) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
// /// 11.14 Programming Exercises pp 337 - 338 #4 ///////////////////////////////////////////////////////////////////////
#include <iostream>
#include <fstream>
#include <string>
#include <iomanip>
using namespace std;
string first[100]{};
string last[100]{};
string ph[100]{};
string bd[100]{};
int num_records = 0;
ifstream fin;
ofstream fout;
void DisplayMenu();
void Find();
void Add();
void Edit();
void Display();
void Sort();
void Update();
int main()
{
// READ THE FILE IN
fin.open("INFO.txt");
while (fin && !fin.eof())
{
fin >> first[num_records]
>> last[num_records]
>> ph[num_records]
>> bd[num_records];
num_records++;
}
fin.close();
DisplayMenu();
}
void DisplayMenu()
{
int menuChoice = 0;
cout << "\t Menu Driven Database\n";
cout << "************************************************\n";
cout << "\t1) Find a person's information\n";
cout << "\t2) Add a person to the database\n";
cout << "\t3) Edit a person's information\n";
cout << "\t4) Display all records to the screen\n";
cout << "\t5) Quit\n**************************************************\n";
cout << "Enter the number of the option you desire: ";
cin >> menuChoice;
switch (menuChoice)
{
case 1:
{
Find();
break;
}
case 2:
{
Add();
break;
}
case 3:
{
Edit();
break;
}
case 4:
{
Display();
break;
}
case 5:
{
Update();
cout << "\tThank you, Goodbye!";
break;
}
}
}
void Find()
{
string temp = " ";
cout << "Enter the Last Name to search for: ";
cin >> temp;
string subString{ temp };
int found = 0;
for (int k = 0; k <= num_records - 1; k++)
{
if (last[k] == subString)
{
cout << "found: " << first[k] << " " << last[k] << "\t\tph: " << ph[k] << "\tDOB: " << bd[k] << "\n";
found++;
}
}
if (found == 0)
cout << "Account not found.\n";
cout << "\n\n";
DisplayMenu();
}
void Add()
{
cout << "Last Name: ";
cin >> last[num_records];
cout << "First Name: ";
cin >> first[num_records];
cout << "ph #: ";
cin >> ph[num_records];
cout << "Date Of Birth: ";
cin >> bd[num_records];
num_records++;
cout << "\n\n";
DisplayMenu();
}
void Edit()
{
Sort();
cout << "\nAll of the current records:\n";
for (int k = 0; k <= num_records - 1; k++)
{
cout << k + 1 << ".\t" << first[k] << " " << last[k] << "\t\tph: " << ph[k] << "\tDOB: " << bd[k] << "\n";
}
int choice = 0;
cout << "\n\nEnter the # of the record you would like to edit: ";
cin >> choice;
cout << "Last Name: ";
cin >> last[choice - 1];
cout << "First Name: ";
cin >> first[choice - 1];
cout << "ph #: ";
cin >> ph[choice - 1];
cout << "Date Of Birth: ";
cin >> bd[choice - 1];
cout << "\n\n";
DisplayMenu();
}
void Display()
{
Sort();
cout << "\nAll of the current records:\n"; //sorted alphabetically by last name
for (int k=0;k<=num_records-1;k++)
{
cout << k + 1 << ".\t" << first[k]<<" "<< last[k]<<"\t\tph: "<<ph[k]<<"\tDOB: "<<bd[k]<<"\n";
}
cout << "\n\n";
DisplayMenu();
}
void Sort()
{
// Sort the records by last name
bool swapped = true;
int j = 0;
string tmp1;
string tmp2;
string tmp3;
string tmp4;
while (swapped)
{
swapped = false;
j++;
for (int i = 0; i < num_records-1; i++)
{
//if (last[i]!=null)
if (last[i] > (last[i + 1]))
{
tmp1 = last[i]; tmp2 = first[i]; tmp3 = ph[i]; tmp4 = bd[i];
last[i] = last[i + 1]; first[i] = first[i + 1]; ph[i] = ph[i + 1]; bd[i] = bd[i + 1];
last[i + 1] = tmp1; first[i + 1] = tmp2; ph[i + 1] = tmp3; bd[i + 1] = tmp4;
swapped = true;
}
}
}
}
void Update()
{
Sort();
fout.open("INFO.txt");
for (int i = 0; i <=num_records-2; i++)
fout << first[i] << " " << last[i] << " " << ph[i] <<" "<< bd[i] << endl;
//fout << first[i] << "\n" << last[i] << "\n" << ph[i] << "\n" << bd[i] << "\n";
fout << first[num_records-1] << " " << last[num_records - 1] << " " << ph[num_records - 1] << " " << bd[num_records - 1];
fout.close();
}
// ///////////////////// RUNS ///////////////////////
*** ORIGINAL FILE ***
Kevin Ashes ph: 765-949-7343 DOB: 06/25/1995
Billy Bobson ph: 951-652-3625 DOB: 12/12/1912
Molly Brown ph: 432-489-7654 DOB: 12/12/1912
David Cackroche ph: 317-981-2527 DOB: 12/25/1928
Kim Cares ph: 343-117-2222 DOB: 11/30/2005
Prince Cheryl ph: 983-554-9000 DOB: 04/12/1988
Trish Dish ph: 798-654-9844 DOB: 06/12/2001
Will Kusick ph: 232-451-2322 DOB: 01/01/2001
Anthony Lei ph: 934-433-9843 DOB: 07/23/1982
Robbie Roberts ph: 317-248-9856 DOB: 03/11/1974
John Smith ph: 123-209-9765 DOB: 11/12/1975
Jon Smith ph: 812-257-9854 DOB: 02/29/1984
James Smyth ph: 650-123-4528 DOB: 05/27/1965
Keel Water ph: 762-848-6543 DOB: 03/25/1997
Tim Wheeler ph: 239-349-3458 DOB: 01/01/1999
*** DISPLAY FROM PROGRAM ***
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 4
All of the current records:
1. Kevin Ashes ph: 765-949-7343 DOB: 06/25/1995
2. Billy Bobson ph: 951-652-3625 DOB: 12/12/1912
3. Molly Brown ph: 432-489-7654 DOB: 12/12/1912
4. David Cackroche ph: 317-981-2527 DOB: 12/25/1928
5. Kim Cares ph: 343-117-2222 DOB: 11/30/2005
6. Prince Cheryl ph: 983-554-9000 DOB: 04/12/1988
7. Trish Dish ph: 798-654-9844 DOB: 06/12/2001
8. Will Kusick ph: 232-451-2322 DOB: 01/01/2001
9. Anthony Lei ph: 934-433-9843 DOB: 07/23/1982
10. Robbie Roberts ph: 317-248-9856 DOB: 03/11/1974
11. John Smith ph: 123-209-9765 DOB: 11/12/1975
12. Jon Smith ph: 812-257-9854 DOB: 02/29/1984
13. James Smyth ph: 650-123-4528 DOB: 05/27/1965
14. Keel Water ph: 762-848-6543 DOB: 03/25/1997
15. Tim Wheeler ph: 239-349-3458 DOB: 01/01/1999
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 2
Last Name: Walter
First Name: Will
ph #: 503-260-5624
Date Of Birth: 06/15/1982
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 4
All of the current records:
1. Kevin Ashes ph: 765-949-7343 DOB: 06/25/1995
2. Billy Bobson ph: 951-652-3625 DOB: 12/12/1912
3. Molly Brown ph: 432-489-7654 DOB: 12/12/1912
4. David Cackroche ph: 317-981-2527 DOB: 12/25/1928
5. Kim Cares ph: 343-117-2222 DOB: 11/30/2005
6. Prince Cheryl ph: 983-554-9000 DOB: 04/12/1988
7. Trish Dish ph: 798-654-9844 DOB: 06/12/2001
8. Will Kusick ph: 232-451-2322 DOB: 01/01/2001
9. Anthony Lei ph: 934-433-9843 DOB: 07/23/1982
10. Robbie Roberts ph: 317-248-9856 DOB: 03/11/1974
11. John Smith ph: 123-209-9765 DOB: 11/12/1975
12. Jon Smith ph: 812-257-9854 DOB: 02/29/1984
13. James Smyth ph: 650-123-4528 DOB: 05/27/1965
14. Will Walter ph: 503-260-5624 DOB: 06/15/1982
15. Keel Water ph: 762-848-6543 DOB: 03/25/1997
16. Tim Wheeler ph: 239-349-3458 DOB: 01/01/1999
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 3
All of the current records:
1. Kevin Ashes ph: 765-949-7343 DOB: 06/25/1995
2. Billy Bobson ph: 951-652-3625 DOB: 12/12/1912
3. Molly Brown ph: 432-489-7654 DOB: 12/12/1912
4. David Cackroche ph: 317-981-2527 DOB: 12/25/1928
5. Kim Cares ph: 343-117-2222 DOB: 11/30/2005
6. Prince Cheryl ph: 983-554-9000 DOB: 04/12/1988
7. Trish Dish ph: 798-654-9844 DOB: 06/12/2001
8. Will Kusick ph: 232-451-2322 DOB: 01/01/2001
9. Anthony Lei ph: 934-433-9843 DOB: 07/23/1982
10. Robbie Roberts ph: 317-248-9856 DOB: 03/11/1974
11. John Smith ph: 123-209-9765 DOB: 11/12/1975
12. Jon Smith ph: 812-257-9854 DOB: 02/29/1984
13. James Smyth ph: 650-123-4528 DOB: 05/27/1965
14. Will Walter ph: 503-260-5624 DOB: 06/15/1982
15. Keel Water ph: 762-848-6543 DOB: 03/25/1997
16. Tim Wheeler ph: 239-349-3458 DOB: 01/01/1999
Enter the # of the record you would like to edit: 15
Last Name: Whater
First Name: Keel
ph #: 762-848-6543
Date Of Birth: 03/25/1997
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 4
All of the current records:
1. Kevin Ashes ph: 765-949-7343 DOB: 06/25/1995
2. Billy Bobson ph: 951-652-3625 DOB: 12/12/1912
3. Molly Brown ph: 432-489-7654 DOB: 12/12/1912
4. David Cackroche ph: 317-981-2527 DOB: 12/25/1928
5. Kim Cares ph: 343-117-2222 DOB: 11/30/2005
6. Prince Cheryl ph: 983-554-9000 DOB: 04/12/1988
7. Trish Dish ph: 798-654-9844 DOB: 06/12/2001
8. Will Kusick ph: 232-451-2322 DOB: 01/01/2001
9. Anthony Lei ph: 934-433-9843 DOB: 07/23/1982
10. Robbie Roberts ph: 317-248-9856 DOB: 03/11/1974
11. John Smith ph: 123-209-9765 DOB: 11/12/1975
12. Jon Smith ph: 812-257-9854 DOB: 02/29/1984
13. James Smyth ph: 650-123-4528 DOB: 05/27/1965
14. Will Walter ph: 503-260-5624 DOB: 06/15/1982
15. Keel Whater ph: 762-848-6543 DOB: 03/25/1997
16. Tim Wheeler ph: 239-349-3458 DOB: 01/01/1999
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 1
Enter the Last Name to search for: Smith
found: John Smith ph: 123-209-9765 DOB: 11/12/1975
found: Jon Smith ph: 812-257-9854 DOB: 02/29/1984
Menu Driven Database
************************************************
1) Find a person's information
2) Add a person to the database
3) Edit a person's information
4) Display all records to the screen
5) Quit
**************************************************
Enter the number of the option you desire: 5
Thank you, Goodbye!
C:\Users\Lenovo\source\repos\Lab9_Schroeder\Debug\Lab9_Schroeder.exe (process 16516) exited with code 0.
To automatically close the console when debugging stops, enable Tools->Options->Debugging->Automatically close the console when debugging stops.
Press any key to close this window . . .
*** NEW FILE DATA ***
Kevin Ashes 765-949-7343 06/25/1995
Billy Bobson 951-652-3625 12/12/1912
Molly Brown 432-489-7654 12/12/1912
David Cackroche 317-981-2527 12/25/1928
Kim Cares 343-117-2222 11/30/2005
Prince Cheryl 983-554-9000 04/12/1988
Trish Dish 798-654-9844 06/12/2001
Will Kusick 232-451-2322 01/01/2001
Anthony Lei 934-433-9843 07/23/1982
Robbie Roberts 317-248-9856 03/11/1974
John Smith 123-209-9765 11/12/1975
Jon Smith 812-257-9854 02/29/1984
James Smyth 650-123-4528 05/27/1965
Will Walter 503-260-5624 06/15/1982
Keel Whater 762-848-6543 03/25/1997
Tim Wheeler 239-349-3458 01/01/1999
|