// Name: msglm // Date: Sep 8th, 2022 // Program Name: Vectirs // Description: Read in a file, output the data to the screen and the student who has the highest credit hours and highest GPA. #include #include #include #include #include #include using namespace std; //initalizes the file void initalize(string location, ifstream & fileVar) { fileVar.open(location); if (!fileVar) { cout << "ERROR! " << location << " failed to open!"; exit(1); } } //Takes in the initalized file and turns it into a vector of arrays of strings vector> serializeIntoVecArr(ifstream & fileVar) { vector> studentDataContainer; studentDataContainer.push_back(array()); int student = 0; int data = 0; while(getline(fileVar, studentDataContainer[student][data]) && fileVar.peek() != EOF) { data = data + 1; if (data == 10) { data = 0; student = student + 1; //This initializes the next set of arrays studentDataContainer.push_back(array()); } } return studentDataContainer; } //Prints out all the data in a tabular format void allData(vector> studentDataContainer) { //this code repetition is gross //Perhaps I could have made this cleaner cout << left << setw(25) << "First Name" << " "; cout << left << setw(25) << "Last Name" << " "; cout << left << setw(25) << "T-Number" << " "; cout << left << setw(25) << "Major" << " "; cout << left << setw(25) << "Advisor" << " "; cout << left << setw(25) << "GPA" << " "; cout << left << setw(25) << "Credit Hours" << " "; cout << left << setw(25) << "Years until Graduation" << " "; cout << left << setw(25) << "Hardest Class" << " "; cout << left << setw(25) << "Easiest Class" << " "; //C++ 11 range based loops //I wish I knew about these earlier for (array student : studentDataContainer) { cout << endl; for (string data : student) { cout << left << setw(25) << data << " "; } } } void highestCreditHours(vector> studentDataContainer) { cout << "\n\nStudent with highest credit hours:\n"; int highestStudent = 0; int highest = 0; //gets the largest using only one pass through for(int studentPos = 0; studentPos < studentDataContainer.size(); studentPos++) { for (string data : studentDataContainer[studentPos]) { if (data[6] > highest) { highest = data[6]; highestStudent = studentPos; } } } //Outputs all the data of the largest for (int dataPos = 0; dataPos < 10; dataPos++) { cout << left << setw(25) << studentDataContainer[highestStudent][dataPos] << " "; } } //clone of highestCreditHours void highestGPA(vector> studentDataContainer) { cout << "\n\nStudent with highest GPA \n"; int highestStudent = 0; int highest = 0; for(int studentPos = 0; studentPos < studentDataContainer.size(); studentPos++) { for (string data : studentDataContainer[studentPos]) { if (data[7] > highest) { highest = data[7]; highestStudent = studentPos; } } } for (int dataPos = 0; dataPos < 10; dataPos++) { cout << left << setw(25) << studentDataContainer[highestStudent][dataPos] << " "; } } int main() { //Variable declaration ifstream data; //"Read in an unknown number of records from an input file and store that data into vector variables for the below" // //The data will be going into a vector, but that vector will hold arrays at a fixed size since each student's data will be fixed //This makes accessing the data infinitely easier initalize("input.txt", data); vector> serializedVectorArray = serializeIntoVecArr(data); allData(serializedVectorArray); highestCreditHours(serializedVectorArray); highestGPA(serializedVectorArray); } /* This program is free software: you can redistribute it and/or modify it under the terms of the GNU Affero General Public License Version 3 ONLY as published by the Free Software Foundation. This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more details. You should have received a copy of the GNU Affero General Public License along with this program. If not, see . */