I'm going to show off my 1337 coding skills right now:
#include <iostream>
#include <fstream>
#include <string>
#include <list>
#include <algorithm>
#include <queue>
#include "Student.h"
using namespace std;
void insert(list<student> &classReg, student &addStudent)
{
if(classReg.empty() == true) //if there's no one in the list, put addStudent at the beginning
{
classReg.insert(classReg.begin(), addStudent);
}
else
{
list<student>::iterator addIt;
addIt = classReg.begin(); //point iterator to beginning of list
while(addIt != classReg.end() && *addIt != addStudent && addStudent > *addIt) //traverse the list with the iterator until it reaches
{ //a student who addStudent comes before in alphabetical order,
addIt++; //or reaches one of several special cases
}
if(addIt == classReg.end()) //if loop reached the end of the list, put addStudent at the end
{ //(May seem redundant, but this is to avoid errors when I dereference addIt below)
classReg.insert(classReg.end(), addStudent);
}
else if(*addIt == addStudent) //if addStudent is already in the class, display an error
{
cout << addStudent.ID << " " << addStudent.name << " is already enrolled in the class. " <<endl;
}
else //else, add them before the student they come before in alphabetical order
{
classReg.insert(addIt, addStudent);
}
}
}
void main()
{
int maxSize;
ifstream infile;
infile.open("Data1.txt",ios::in);
if (!infile)
cout << "Not a file." << endl;
else
{
list<student> classReg;
deque<student> wait;
while(infile.eof()!=true)
{
string transType;
infile >> transType;
if(transType=="setMax")
{
infile >> maxSize;
}
else if (transType == "drop")
{
int studentID;
infile >> studentID;
student dropStudent;
dropStudent.name = "NULL";
dropStudent.ID = studentID;
list<student>::iterator dropIt;
dropIt = find(classReg.begin(), classReg.end(), dropStudent); //makes dropIt point to the student in question
if(dropIt == classReg.end()) //if nothing is found, send an error.
{
cout << "Cannot drop class because " << studentID << " is not enrolled." << endl;
}
else
{
classReg.erase(dropIt);
if(wait.empty() != true)
{
student waitStudent;
waitStudent = wait.front();
insert(classReg, waitStudent); //insert the person at the front of the queue into the list
cout << waitStudent.ID << " " << waitStudent.name<< " has been removed from waiting list and enrolled in class."<<endl;
wait.pop_back(); //remove them from the queue
}
}
}
else if (transType == "add")
{
student addStudent;
infile >> addStudent.ID;
string Name;
string lastName;
infile >> Name;
infile >> lastName;
Name.append(" ");
Name.append(lastName);
addStudent.name = Name;
if(classReg.size() > maxSize) //if list is larger than the maximum size push the addStudent to the waiting queue
{
wait.push_front(addStudent);
cout << "Class is full; student " << addStudent.ID << " " << addStudent.name << " has been added to waiting list." << endl;
}
else
{
insert(classReg, addStudent);
}
}
else if (transType == "printRoll")
{
if(classReg.empty() == true)
{
cout << "Class roll: No students enrolled at this time." << endl;
}
else
{
list<student>::iterator printIt;
for(printIt = classReg.begin(); printIt != classReg.end(); printIt++)
{
student printStudent;
printStudent = *printIt;
cout << printStudent.ID << " " << printStudent.name << endl;
}
}
}
else if (transType == "printWait")
{
if(wait.empty() == true)
{
cout<<" Waiting list is currently empty."<<endl;
}
else
{
deque<student>::iterator printIt;
for(printIt = wait.begin(); printIt != wait.end(); printIt++)
{
student printStudent;
printStudent = *printIt;
cout << printStudent.ID << " " << printStudent.name << endl;
}
}
}
}
}
system("pause");
}
Took me 6 hours to complete. And it's still buggy. I imagine that if anyone has experience in C++ coding, they're going to quote this and make fun of me.