summaryrefslogtreecommitdiffstats
path: root/C++/Vectors/Vectors.cpp
blob: 67adaaedb8e423bf361360ae93c88fff72c0ceb5 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// 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 <iostream>
#include <fstream>
#include <string>
#include <vector>
#include <array>
#include <iomanip>
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<array<string, 10>> serializeIntoVecArr(ifstream & fileVar) {
    vector<array<string, 10>> studentDataContainer;
    studentDataContainer.push_back(array<string, 10>());
    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<string, 10>());
        }
    }
    return studentDataContainer;
}


//Prints out all the data in a tabular format
void allData(vector<array<string, 10>> 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<string, 10> student : studentDataContainer) {
        cout << endl;
        for (string data : student) {
            cout << left << setw(25) << data << " ";
        }
    }

}



void highestCreditHours(vector<array<string, 10>> 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<array<string, 10>> 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<array<string, 10>> 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 <https://www.gnu.org/licenses/>.
   */