Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Leslie Jo
- Assignment 3: Student Database that can Import/Export .txt files and is both searchable & sortable.
- 4/12/2018
- */
- #include<iostream>
- #include<string>
- #include<vector>
- #include<iomanip>
- #include<cctype>
- #include<fstream>
- #include<algorithm>
- #include<cstdlib>
- using namespace std;
- //This function displays the content of the vector.
- void displayVector(vector<int>, const vector<string>, const vector<string>);
- //Validates integer to determine matches for sorting purposes.
- int validateInteger(int);
- //Converts string to all uppercases.
- string convertUppercase(string sTemp);
- void tripleSort(vector<string>&vSClass1, vector<string>&vSClass2, vector<string>&vSClass3);
- int binarySearch(vector <string> &, string, int, int);
- int main()
- {
- int iStudentNumber = 0, i_temp;
- string sUserInput, sTemp, sFName, sMName, sLName, sFullName, sID, sClass;
- ifstream inputFile;
- ofstream outputFile;
- vector<string> vName, vFName, vMName, vLName, vID, vClass, vSInput1, vSInput2, vSInput3;
- //IMPORTING is also READING the file. EXPORTING is also writing the file.
- const char
- IMPORTING = 'A',
- ADDING = 'B',
- DISPLAYING = 'C',
- SORTING = 'D',
- EXPORTING = 'E',
- SEARCHING = 'F',
- ENDING = 'G';
- do
- {
- // Display A-G Menu Options
- cout << "\tStudent Enrollment Application" << endl << endl;
- cout << "A. Importing the Student List from an external text file" << endl;
- cout << "B. Adding Student's Information into the Student List" << endl;
- cout << "C. Displaying the Content of the Student List from the Database" << endl;
- cout << "D. Sorting the Content of the Student List from a Database" << endl;
- cout << "E. Exporting the Student List to an External Text File" << endl;
- cout << "F. Searching for a Student's Information from the Student List" << endl;
- cout << "G. Ending the Program" << endl << endl;
- cout << "What would you like to perform?: ";
- getline(cin, sTemp);
- switch (toupper(sTemp[0]))
- {
- case IMPORTING:
- cout << "Please enter the password: ";
- getline(cin, sTemp);
- inputFile.open(sTemp.c_str());
- while (inputFile.fail())
- {
- inputFile.clear();
- cout << "Open file error!Please re-enter: ";
- getline(cin, sTemp);
- inputFile.open(sTemp.c_str());
- }
- while (inputFile >> sFName >> sMName >> sLName >> sID >> sClass)
- { //Performed the .txt reading. Don't open file yet. Remove the sentinel.
- if (sMName == "|")
- sFullName = sFName + " " + sLName;
- else
- sFullName = sFName + " " + sMName + " " + sLName;
- //Erases the | in the middle name.
- cout << left << setw(30) << sFullName << setw(10) << sID << setw(10) << sClass << endl;
- vName.push_back(convertUppercase(sFullName));
- vID.push_back(convertUppercase(sID));
- vClass.push_back(convertUppercase(sClass));
- }
- cout << "Reading file successfully!";
- inputFile.close();
- break;
- case ADDING:
- cout << "\nTheStudent List starts with the size of: " << vName.size();
- cout << "\n\nHow many students would you like to add: ";
- cin >> iStudentNumber;
- iStudentNumber = validateInteger(iStudentNumber);
- cin.ignore();
- for (int i = 1; i <= iStudentNumber; i++)
- {
- cout << "\nStudent " << i << ":";
- cout << "\n\nEnter the Student's Full Name: \t";
- getline(cin, sFullName);
- sFullName = convertUppercase(sFullName);
- vName.push_back(sFullName);
- cout << "\nEnter the Student's ID Number: \t";
- //Always true.
- while (getline(cin, sID) && sID.size() != 7)
- {
- cout << "Please enter a valid value" << endl;
- }
- vID.push_back(sID);
- for (int i = 0; i < sID.size(); ++i) {
- sID[i];
- }
- cout << "\nEnter the Student's Class: \t";
- getline(cin, sClass);
- sClass = convertUppercase(sClass);
- vClass.push_back(sClass);
- }
- break;
- case DISPLAYING:
- //Use for loop. Know the size of each vector using sizeNumber function.
- cout << "The list has the size of: " << vName.size() << endl;
- for (int i = 0; i < vName.size(); i++)
- cout << left << setw(40) << vName[i] << setw(10) << vID[i] << setw(10) << vClass[i] << endl;
- break;
- case SORTING:
- //Study dualsort function. Ch 8 Problem 8-6
- cout << "1 for sorting by name, 2 for sorting by ID: ";
- getline(cin, sTemp);
- if (sTemp == "1")
- tripleSort(vName, vID, vClass);
- else if (sTemp == "2")
- tripleSort(vID, vName, vClass);
- break;
- case EXPORTING:
- //Write vectors to external file.
- cout << "Enter the name for the file to be opened:";
- getline(cin, sTemp);
- outputFile.open(sTemp.c_str());
- for (int i = 0; i < vName.size(); i++)
- cout << left << setw(40) << vName[i] << setw(10) << vID[i] << setw(10) << vClass[i] << endl;
- cout << "Writing to the file " << sTemp << " was successful.";
- outputFile.close();
- break;
- case SEARCHING:
- //Needs SORTING completed first.
- cout << "\n1 for searching by name, 2 for searching by ID: ";
- getline(cin, sTemp);
- if (sTemp == "1")
- {
- cout << "\nPlease enter name: ";
- getline(cin, sTemp);
- i_temp = binarySearch(vName, sTemp, 0, vName.size());
- if (i_temp!= -1)
- {
- cout << "\nMatching Data found\n";
- cout << left << setw(40) << vName[i_temp] << setw(10) << vID[i_temp] << setw(10) << vClass[i_temp] << endl;
- }
- else
- cout << "\nMatching Data not found\n";
- }
- else if (sTemp == "2")
- {
- cout << "\nPlease enter ID: ";
- getline(cin, sTemp);
- i_temp = binarySearch(vID, sTemp, 0, vID.size());
- if (i_temp != -1)
- {
- cout << "\nMatching Data found";
- cout << left << setw(40) << vName[i_temp] << setw(10) << vID[i_temp] << setw(10) << vClass[i_temp] << endl;
- }
- else
- cout << "\nMatching Data not found";
- }
- break;
- default:
- cout << " \n Invalid Input!";
- }
- } while (toupper(sTemp[0]) != ENDING);
- return 0;
- }
- string convertUppercase(string sInput)
- {
- for (int i = 0; i < sInput.length(); i++)
- sInput[i] = toupper(sInput[i]);
- return sInput;
- }
- int validateInteger(int iInput)
- {
- while (cin.fail())
- {
- cin.clear();
- cin.ignore(INT_MAX, '\n');
- cout << "Invalid input. Please try again: ";
- cin >> iInput;
- }
- return iInput;
- }
- void displayVector(vector<string> vInput)
- {
- for (unsigned int i = 0; i < vInput.size(); i++)
- cout << vInput[i] << "\t";
- }
- void tripleSort(vector<string>&vSClass1, vector<string>&vSClass2, vector<string>& vSClass3)
- {
- int iTemp;
- string sTemp1, sTemp2, sTemp3;
- for (int i = 0; i < (vSClass1.size() - 1); i++)
- {
- iTemp = i;
- sTemp1 = vSClass1[i];
- sTemp2 = vSClass2[i];
- sTemp3 = vSClass3[i];
- for (int j = i + 1; j < vSClass1.size(); j++)
- {
- if (vSClass1[j] < sTemp1)
- {
- sTemp1 = vSClass1[j];
- sTemp2 = vSClass2[j];
- sTemp3 = vSClass3[j];
- iTemp = j;
- }
- }
- vSClass1[iTemp] = vSClass1[i];
- vSClass2[iTemp] = vSClass2[i];
- vSClass3[iTemp] = vSClass3[i];
- vSClass1[i] = sTemp1;
- vSClass2[i] = sTemp2;
- vSClass3[i] = sTemp3;
- }
- }
- // Recursive Binary Search Algorithm:
- int binarySearch(vector <string> &vName, string sSearch, int min, int max)
- {
- if (max < min)
- return -1;
- else
- {
- // Calculate MidPoint to cut the Array in half
- int mid = (min + max) / 2;
- // Three way comparison
- if (vName[mid] > sSearch)
- // Search Value is in the Lower Subset
- return binarySearch(vName, sSearch, min, mid - 1);
- else if (vName[mid] < sSearch)
- // Search Value is in the Upper Subset
- return binarySearch(vName, sSearch, mid + 1, max);
- else
- // Search Value has been found
- return mid;
- }
- }
Add Comment
Please, Sign In to add comment