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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269
|
/*
This is a program that utilizes vectors and sorting to print out 4 instances of student information based on first name, last name, gpa and major.
StudentMain.cpp
*/
#include <iostream>
#include <fstream>
#include <string>
#include <vector> // declares the vector
#include "Student.h"
using namespace std;
void fill(vector<Student> &v, int &l);
void printMenu();
void sortBylName(vector<Student> &v);
void sortByMajor(vector<Student> &v);
void sortByGPA(vector<Student> &v);
void searchByName(vector<Student> &v, string fn, string ln, int l);
int main()
{
vector<Student> vec(10, Student()); // Here a vector with a size of 10 is filled with type Student
string lastName, firstName, Major;
double GPA;
int length = 0;
int choice;
do
{
printMenu();
// Below is a switch that will display the menu for the user
switch (choice)
{
case 1: fill(vec, length);
int temp = vec.size() - length;
for (int i = temp; i < vec.size(); i++)
{
vec[i].print();
}
cout << endl;
case 2: sortBylName(vec);
int temp = vec.size() - length;
for (int i = temp; i < vec.size(); i++)
{
vec[i].print();
}
cout << endl;
case 3:
sortByGPA(vec);
int temp = vec.size() - length;
for (int i = temp; i < vec.size(); i++)
{
vec[i].print();
}
cout << endl;
case 4:
sortByMajor(vec);
int temp = vec.size() - length;
for (int i = temp; i < vec.size(); i++)
{
vec[i].print();
}
cout << endl;
case 5:
cout << "Enter the Student first and last name you would like to search: ";
cin >> firstName >> lastName;
cout << endl;
case 6:
cout << "Enter the Student Firstname:" << endl;
cin >> firstName;
cout << "Enter the Student Lastname:" << endl;
cin >> lastName;
cout << "Enter the Student GPA:" << endl;
cin >> GPA;
cout << "Enter the Student Major:" << endl;
cin >> Major;
Student s1(lastName, firstName, Major, GPA);
vec.push_back(s1);
length++;
fill(vec, length);
case 7:
cout << "Please Enter the First and Last Name of the Student You Would Like to Search For: ";
cin >> firstName >> lastName;
cout << endl;
searchByName(vec, firstName, lastName, length);
Student s2(lastName, firstName, Major, GPA);
default:
cout << "Invalid Entry";
}
} while (choice < 0);
cout << "Please Enter the Student's First Name: " << endl;
cin >> firstName;
cout << "Please Enter the Student's Last Name: " << endl;
cin >> lastName;
cout << "Please Enter the Student's GPA: " << endl;
cin >> GPA;
cout << "Please Enter the Student's Major: " << endl;
cin >> Major;
Student s1(lastName, firstName, Major, GPA);
vec.push_back(s1); // This adds a new element at the end of the vector
length++;
fill(vec, length);
sortBylName(vec);
int temp = vec.size() - length;
for (int i = temp; i < vec.size(); i++)
{
vec[i].print();
}
cout << endl;
system("pause");
return 0;
}
void printMenu()
{
cout << "1. Display the Student List" << endl;
cout << "2. Sort List by Last Name" << endl;
cout << "3. Sort List by GPA" << endl;
cout << "4. Sort List by Major" << endl;
cout << "5. Search by Name" << endl;
cout << "6. Add Student Data" << endl;
cout << "7. Remove Student Data" << endl;
}
void fill(vector<Student> &v, int &l)
{
ifstream inFile;
inFile.open("students.txt");
string strlName, strfName, strMajor;
double GPA;
while (!inFile.eof())
{
inFile >> strlName >> strfName >> GPA >> strMajor;
Student temp(strlName, strfName, strMajor, GPA);
v.push_back(temp);
l++;
}
inFile.close();
}
void sortBylName(vector<Student> &v)
{
Student temp;
int location;
for (int i = 1; i < v.size(); i++)
{
if (v[i].getlName() < v[i - 1].getlName())
{
temp = v[i];
location = i;
do
{
v[i] = v[location - 1];
location--;
} while (location > 0 && v[location - 1].getlName() > temp.getlName());
v[location] = temp;
}
}
}
void sortByMajor(vector<Student> &v)
{
Student temp;
int location;
for (int i = 1; i < v.size(); i++)
{
if (v[i].getmajor() < v[i - 1].getmajor())
{
temp = v[i];
location = i;
do
{
v[i] = v[location - 1];
location--;
} while (location > 0 && v[location - 1].getmajor() > temp.getmajor());
v[location] = temp;
}
}
}
void sortByGPA(vector<Student> &v)
{
Student temp;
int location;
for (int i = 1; i < v.size(); i++)
{
if (v[i].getgpa < v[i - 1].getgpa())
{
temp = v[i];
location = i;
do
{
v[i] = v[location - 1];
location--;
} while (location > 0 && v[location - 1].getgpa() > temp.getgpa());
v[location] = temp;
}
}
}
void searchByName(vector<Student> &v, string fn, string ln, int l)
{
bool found = false;
int temp = v.size() - l;
for (int i = temp; i < v.size(); i++)
{
if (v[i].getlName() == ln)
{
if (v[i].getfName() == fn)
{
found = true;
cout << "The Student " << fn << " " << ln << "is at position " << i << endl;
v[i].print();
return;
}
}
}
if (found = false)
cout << "The Student does not exist" << endl;
}
|