Sorry for the long post of code, but I have a programming assignment that is supposed to focus on heaps, inheritance, and I/O and command line arguments. I think I have the heaps and inheritance down but I'm still receiving a segmentation fault in the timestepping where (if time == inOffice->departuretime) and I have no idea.
I'm also pasting the test.txt file for further reference. Thanks in advance for any help or typos you can find.
Code:
/*
This is the Office Hours simulation program. It simulates what happens
during a professor's office hours, with students lining up and waiting.
"Students" are read through a text file that contains lines of two ints,
the arrival time and request time.
The simulation is to calculate the average wait time of all the students
in one text file, the wait time being how long a student has to wait before
he/she enters the office. This is done through a heap, with the priority
of each student being 1) the time they arrive (First Come First Served) and
then 2) their request time (Shortest Request First).
*/
#include <iostream>
#include <fstream>
#include "heap.h"
#include <string>
using namespace std;
class student : public HeapItem {
public:
int ID;
int arrivaltime;
int requesttime;
int waitingtime;
int departuretime;
int enteredOffice;
student(int who, int a, int r);
};
student::student(int who, int a, int r) {
ID = who;
arrivaltime = a;
requesttime = r;
}
int main(int argc, char *argv[]) {
char inputFilename[] = "test.txt";
ifstream inFile;
int rt;
int at;
int sumInQueue = 0;
int averageWaitTime;
Heap of;
student *next = NULL;
student *inOffice = NULL;
inFile.open(inputFilename, ios::in); // Open up file
// Error check.
if (!inFile)
cerr << "Can't open input file " << inputFilename << endl;
int studentnumber = 0; // Will give unique ID to each student
// timestep
inFile >> at >> rt;
student first = student(studentnumber, at, rt);
studentnumber++;
inOffice = &first;
inOffice->enteredOffice = 0;
inOffice->departuretime = inOffice->requesttime + inOffice->enteredOffice;
inFile >> at >> rt;
student second = student(studentnumber, at, rt);
studentnumber++;
next = &second;
for (int time = 0; (next != NULL or !(of.isEmpty()) or
inOffice != NULL) and !inFile.eof(); time++) {
inFile >> at >> rt;
student a = student(studentnumber, at, rt);
studentnumber++;
// if time == arrival time of the next student
if (time == next->arrivaltime and next != NULL) {
// Insert student into heap
of.insertHeapItem(a, at);
// Report this event
cout << time << " Student " << a.ID << " enters queue.\n";
// Read next student, if one exists, and calculate time
if (next != NULL) {
HeapItem m = of.removeMin();
student &n = static_cast<student&>(m);
next = &n;
}
else
next = &a;
}
// If time == departure time
if (time == inOffice->departuretime) {
// Calculate wait time
sumInQueue = sumInQueue +
(inOffice->enteredOffice - inOffice->arrivaltime);
// Report the event
cout << time << " Student " << inOffice->ID << " leaves office.\n";
// remove the student from the office
inOffice = NULL;
}
// If no student in office and heap is not empty
if (inOffice = NULL and of.isEmpty() == false) {
// Remove a student from heap and put into office
HeapItem h = of.removeMin();
student s = static_cast<student&>(h);
inOffice = &s;
inOffice->enteredOffice = time;
// Calculates departure time
inOffice->departuretime = inOffice->requesttime
+ inOffice->enteredOffice;
// Report this
cout << time << " Student " << inOffice->ID << " enters office\n";
}
}
return 0;
}