// CST116F2021-Lab9_Schroeder.cpp : This file contains the 'main' function. Program execution begins and ends there. // /* //// 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"; } */ /* // /// 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++; } fin.close(); } */ // /// 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() { /*int SearchBy = 0; cout << "1) Last Name: "; cout << "2) First Name: "; cout << "3) ph #: "; cout << "4) Date Of Birth: "; cout << "Which field would you like to search by? (Enter a # 1 thru 4)"; cin >> SearchBy;*/ Sort(); string subString = ""; cout << "Enter the Last Name of the person whose account you wish to see:"; cin >> subString; bool found = false; int left = 0; int right = num_records; int mid = 0; while (left <= right && found != true) { int mid = (left + right / 2); if (subString == last[mid]) { cout << "found: " << first[mid] << " " << last[mid] << "\t\tph: " << ph[mid] << "\tDOB: " << bd[mid] << "\n\n\n"; found = true; } else if (subString > last[mid]) left = mid + 1; else right = mid - 1; } if (found != true) cout << "The account was not found.\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] > (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 - 1; 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.close(); }