// 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() { 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(); }