// CST116F2021-Lab9.cpp : This file contains the 'main' function. Program execution begins and ends there. // #include #include #include #include using namespace std; const int ARRAY_SIZE = 4; const int NUM_RECORDS = 100; const int NAME_SIZE = 100; int menuSelect(int); void getInput(char[NAME_SIZE]); void readFile(char[NAME_SIZE], string[][ARRAY_SIZE], int&); void findData(string[][ARRAY_SIZE], int&, int); void addData(string[][ARRAY_SIZE], int&); void sortData(string[][ARRAY_SIZE], int); void editData(string[][ARRAY_SIZE], int, int); void dispData(string[][ARRAY_SIZE], int); void writeFile(string[][ARRAY_SIZE], int, char[NAME_SIZE]); int main() { int menu = 0, loc = 0, size = 0; char file_name[NAME_SIZE]{}; string info[NUM_RECORDS][ARRAY_SIZE]; getInput(file_name); readFile(file_name, info, size); sortData(info, size); while (menu != 5) { menu = menuSelect(menu); switch (menu) { default: { cout << "Invalid menu selection." << endl; break; } case 1: { findData(info, loc, size); break; } case 2: { addData(info, size); break; } case 3: { editData(info, loc, size); break; } case 4: { dispData(info, size); break; } case 5: { writeFile(info, size, file_name); break; } } } cout << "Exiting Program." << endl; return 0; } void getInput(char inputFileName[]) { cout << "Please enter the file name: "; cin >> inputFileName; } void readFile(char readFileName[], string readArray[][ARRAY_SIZE], int& readSize) { int i = 0, j = 0; ifstream inFile; inFile.open(readFileName); if (inFile.is_open()) { cout << "File is opened." << endl; while (!inFile.eof() && i < NUM_RECORDS) { while (j < ARRAY_SIZE) { if (j == 3) getline(inFile, readArray[i][j], '\n'); else getline(inFile, readArray[i][j], ' '); j++; } j = 0; i++; } readSize = i; } inFile.close(); return; } int menuSelect(int inputMenu) { cout << "Please select from the following options:" << endl << "\t1) Find a person's information" << endl << "\t2) Add a person to the database" << endl << "\t3) Edit a person's information" << endl << "\t4) Display all records to the screen" << endl << "\t5) Quit" << endl; cin >> inputMenu; return inputMenu; } void findData(string findArray[][ARRAY_SIZE], int& findLoc, int findSize) { int i = 0; int found = false; string name = {}; cout << "Please enter the last name of the user you are looking for: "; cin >> name; while (i < findSize && found == 0) { if (name.compare(findArray[i][0]) == 0) { found = true; findLoc = i; } i++; } if (!found) cout << "Entry does not exist in database." << endl; else { for (int j = 0; j < ARRAY_SIZE; j++) cout << findArray[findLoc][j] << ' '; cout << endl; } } void addData(string addArray[][ARRAY_SIZE], int& addSize) { int i = addSize; cout << "Please enter the last name: "; cin >> addArray[i][0]; cout << "Please enter the first name: "; cin >> addArray[i][1]; cout << "Please enter the phone number in format ###-###-####: "; cin >> addArray[i][2]; cout << "Please enter the birthday in format mm-dd-yyyy: "; cin >> addArray[i][3]; addSize += 1; sortData(addArray, addSize); } void sortData(string sortArray[][ARRAY_SIZE], int sortSize) { int i = 0, j = 1, k = 0, l = 0; string temp; char first1 = {}, first2 = {}; while (l < sortSize) { while (i < sortSize - 1) { if (sortArray[l][0] != "\0") { first1 = sortArray[i][0].front(); if (sortArray[j][0] != "\0") { first2 = sortArray[j][0].front(); if (first1 > first2) { while (k < ARRAY_SIZE) { temp = sortArray[i][k]; sortArray[i][k] = sortArray[j][k]; sortArray[j][k] = temp; k++; } k = 0; } } } i++; j++; } i = 0; j = 1; l++; } } void editData(string editArray[][ARRAY_SIZE], int editLoc, int editSize) { int select = 0; findData(editArray, editLoc, editSize); cout << "Please select from the following options:" << endl << "\t1) Change Last Name" << endl << "\t2) Change First Name" << endl << "\t3) Change Phone Number" << endl << "\t4) Change Birthday" << endl << "\t5) Quit" << endl; cin >> select; switch (select) { default: { cout << "Invalid selection." << endl; break; } case 1: { cout << "Please enter the last name: "; cin >> editArray[editLoc][0]; break; } case 2: { cout << "Please enter the first name: "; cin >> editArray[editLoc][1]; break; } case 3: { cout << "Please enter the phone number in format ###-###-####: "; cin >> editArray[editLoc][2]; break; } case 4: { cout << "Please enter the birthday in format mm-dd-yyyy: "; cin >> editArray[editLoc][3]; break; } case 5: { cout << "No changes made." << endl; break; } } if (select > 0 && select <= 4) sortData(editArray, editSize); } void dispData(string dispArray[][ARRAY_SIZE], int dispSize) { cout << setw(20) << "Entry Number" << setw(20) << "Last Name" << setw(20) << "First Name" << setw(20) << "Phone Number" << setw(20) << "Birthday" << endl; for (int i = 0; i < dispSize; i++) { cout << setw(20) << i + 1; for (int j = 0; j < ARRAY_SIZE; j++) { cout << setw(20) << dispArray[i][j]; } cout << endl; } } void writeFile(string writeArray[][ARRAY_SIZE], int writeSize, char writeName[NAME_SIZE]) { int i = 0, j = 0; ofstream outFile; outFile.open(writeName); if (outFile.is_open()) { while (i < writeSize) { while (j < ARRAY_SIZE) { if(j != 3) outFile << writeArray[i][j] << ' '; else outFile << writeArray[i][j] << endl; j++; } j = 0; i++; } } outFile.close(); } //PROGRAM 2 //#include //#include //#include // //using namespace std; // //#define ARRAY_SIZE 100 // //void getInput(char[ARRAY_SIZE]); //void readFile(char[ARRAY_SIZE]); // //int main() //{ // char file_name[ARRAY_SIZE]; // string sentences[ARRAY_SIZE]; // // getInput(file_name); // readFile(file_name); // // return 0; //} // //void getInput(char inputFileName[]) //{ // cout << "Please enter the file name: "; // cin >> inputFileName; //} // //void readFile(char readFileName[]) //{ // int i = 1, num_char = 0; // string temp; // // // ifstream inFile; // inFile.open(readFileName); // // if (inFile.is_open()) // { // cout << "File is opened." << endl; // while (!inFile.eof()) // { // getline(inFile, temp, '\n'); // num_char = temp.length(); // cout << i << ' ' << temp << ' ' << num_char << " characters in this string." << endl; // i++; // } // } // inFile.close(); // return; //} //PROGRAM 1 //#include //#include //#include //#include // //using namespace std; // //#define ARRAY_SIZE 100 //#define MAX_VALUES 10 // //void getInput(char[ARRAY_SIZE]); //void readFile(int[MAX_VALUES], char[ARRAY_SIZE]); //void findHiLo(int[MAX_VALUES]); //void sortAscending(int[MAX_VALUES]); // //int main() //{ // // // char file_name[ARRAY_SIZE]{'\0'}; // int integers[MAX_VALUES]{}; // // getInput(file_name); // readFile(integers, file_name); // findHiLo(integers); // sortAscending(integers); // return 0; // //} // //void getInput(char inputFileName[]) //{ // cout << "Please enter the file name: "; // cin >> inputFileName; //} // //void readFile(int readArray[], char readFileName[]) //{ // int i = 0; // string temp_s; // int temp_i = 0; // // ifstream inFile; // inFile.open(readFileName); // // if (inFile.is_open()) // { // cout << "File is opened." << endl; // while (!inFile.eof()) // { // while (i < MAX_VALUES) // { // getline(inFile, temp_s, ' '); // temp_i = stoi(temp_s); // readArray[i] = temp_i; // i++; // } // } // } // inFile.close(); // return; //} // //void findHiLo(int findArray[]) //{ // int i = 1, high = 0, low = 0; // low = findArray[0]; // high = findArray[0]; // while (i < MAX_VALUES) // { // if (low > findArray[i]) // low = findArray[i]; // if (high < findArray[i]) // high = findArray[i]; // i++; // } // // cout << "The largest number is " << high << " and the smallest number is " << low << endl; //} // //void sortAscending(int sortArray[]) //{ // int i = 0, j = 0, temp = 0; // // while (i < MAX_VALUES) // { // j = i + 1; // while (j < MAX_VALUES) // { // if (sortArray[i] > sortArray[j]) // { // temp = sortArray[i]; // sortArray[i] = sortArray[j]; // sortArray[j] = temp; // } // j++; // } // i++; // } // // i = 0; // while (i < MAX_VALUES) // { // cout << sortArray[i] << endl; // i++; // } //}