Lab 9 CST116 Benjamin Schroeder /////////////////////////////////////////////////////////////////////////////////////////////////////////////// //// 11.14 Programming Exercises pp 337 - 338 #2 ////////////////////////////////////////////////////////////////// #include #include #include #include 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 #include #include #include 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 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 #include #include #include 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: "< (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