diff options
Diffstat (limited to 'CST116F2021-Lab9/CST116F2021-Lab9.cpp')
| -rw-r--r-- | CST116F2021-Lab9/CST116F2021-Lab9.cpp | 248 |
1 files changed, 160 insertions, 88 deletions
diff --git a/CST116F2021-Lab9/CST116F2021-Lab9.cpp b/CST116F2021-Lab9/CST116F2021-Lab9.cpp index 3499469..f0034dc 100644 --- a/CST116F2021-Lab9/CST116F2021-Lab9.cpp +++ b/CST116F2021-Lab9/CST116F2021-Lab9.cpp @@ -14,56 +14,62 @@ const int NAME_SIZE = 100; int menuSelect(int); void getInput(char[NAME_SIZE]); -void readFile(char[NAME_SIZE], string[][ARRAY_SIZE]); -void findData(string[][ARRAY_SIZE]); -void addData(string[][ARRAY_SIZE]); -void sortData(string[][ARRAY_SIZE]); -void editData(string[][ARRAY_SIZE]); -void dispData(string[][ARRAY_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; + int menu = 0, loc = 0, size = 0; char file_name[NAME_SIZE]{}; string info[NUM_RECORDS][ARRAY_SIZE]; getInput(file_name); - readFile(file_name); + readFile(file_name, info, size); while (menu != 5) - menuSelect(menu); - - switch (menu) - { - default: - { - cout << "Invalid menu selection." << endl; - break; - } - case 1: - { - findData(info); - break; - } - case 2: { - addData(info); - break; - } - case 3: - { - editData(info); - break; - } - case 4: - { - dispData(info); - break; - } - case 5: - break; + 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; + } + } } - //Before program closes, files must be updated with the new database stored in 'info' + cout << "Exiting Program." << endl; return 0; } @@ -74,29 +80,33 @@ void getInput(char inputFileName[]) cin >> inputFileName; } -void readFile(char readFileName[], string readArray[][ARRAY_SIZE]) +void readFile(char readFileName[], string readArray[][ARRAY_SIZE], int& readSize) { int i = 0, j = 0; - string temp; ifstream inFile; inFile.open(readFileName); + if (inFile.is_open()) { cout << "File is opened." << endl; - while (!inFile.eof()) + while (!inFile.eof() && i < NUM_RECORDS) { - //add a counter i for the number of lines in the inFile while (j < ARRAY_SIZE) { - getline(inFile, temp, ' '); - readArray[i][j] = temp; + if (j == 3) + getline(inFile, readArray[i][j], '\n'); + else + getline(inFile, readArray[i][j], ' '); j++; } + j = 0; + i++; } + readSize = i; } - inFile.close(); //going to be editting the inFile so don't close yet + inFile.close(); return; } @@ -108,60 +118,98 @@ int menuSelect(int inputMenu) return inputMenu; } -void findData(string findArray[][ARRAY_SIZE]) +void findData(string findArray[][ARRAY_SIZE], int& findLoc, int findSize) { - string name; - int found = false, left = 0, right = sizeof(findArray), mid = 0; - + int i = 0; + int found = false; + string name = {}; + cout << "Please enter the last name of the user you are looking for: "; cin >> name; - while (!found && left <= right) - { + + 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; } - //Find function needs to specify the location of the user for edit function } -void addData(string addArray[][ARRAY_SIZE]) +void addData(string addArray[][ARRAY_SIZE], int& addSize) { - int i = 0; + int i = addSize; + + cout << "Please enter the last name: "; + cin >> addArray[i][0]; - while (i < NUM_RECORDS) - { - if (addArray[i][0] == "\0") //Should stop at the first empty entry - { - cout << "Please enter the last name: "; - cin >> addArray[i][0]; + cout << "Please enter the first name: "; + cin >> addArray[i][1]; - 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 phone number in format ###-###-####: "; - cin >> addArray[i][2]; + cout << "Please enter the birthday in format mm-dd-yyyy: "; + cin >> addArray[i][3]; - cout << "Please enter the birthday in format mm-dd-yyyy: "; - cin >> addArray[i][3]; - break; - } - else - i++; - } - sortData(addArray); + addSize += 1; + sortData(addArray, addSize); } -void sortData(string sortArray[][ARRAY_SIZE]) +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]) +void editData(string editArray[][ARRAY_SIZE], int editLoc, int editSize) { - int select = 0, i = 0; - findData(editArray); - //Find function needs to give a value for i so the program correctly edits the specified user - + 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; @@ -176,25 +224,25 @@ void editData(string editArray[][ARRAY_SIZE]) case 1: { cout << "Please enter the last name: "; - cin >> editArray[i][0]; + cin >> editArray[editLoc][0]; break; } case 2: { cout << "Please enter the first name: "; - cin >> editArray[i][1]; + cin >> editArray[editLoc][1]; break; } case 3: { cout << "Please enter the phone number in format ###-###-####: "; - cin >> editArray[i][2]; + cin >> editArray[editLoc][2]; break; } case 4: { cout << "Please enter the birthday in format mm-dd-yyyy: "; - cin >> editArray[i][3]; + cin >> editArray[editLoc][3]; break; } case 5: @@ -204,15 +252,15 @@ void editData(string editArray[][ARRAY_SIZE]) } } if (select > 0 && select <= 4) - sortData(editArray); + sortData(editArray, editSize); } -void dispData(string dispArray[][ARRAY_SIZE]) +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 < sizeof(dispArray); i++) //Should only display filled entries + for (int i = 0; i < dispSize; i++) { - cout << setw(20) << i; + cout << setw(20) << i + 1; for (int j = 0; j < ARRAY_SIZE; j++) { cout << setw(20) << dispArray[i][j]; @@ -221,6 +269,30 @@ void dispData(string dispArray[][ARRAY_SIZE]) } } +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 << left << setw(sizeof(writeArray[i][j]) + 1) << writeArray[i][j] << ' '; + else + outFile << left << setw(sizeof(writeArray[i][j])+ 1) << writeArray[i][j] << endl; + } + } + } + + outFile.close(); +} + //PROGRAM 2 //#include <iostream> |