I am writing code to simulate a queue at a bank, which calculates the average wait time of all customers that enter the queue.
I'm getting a STATUS_ACCESS_VIOLATION at line 56 of program3.cpp. I've had this error before and I'm pretty sure it happens because of an attempt to act on a NULL value, but I can't quite figure it out.
/* ---program3.cpp------------------------------------------------------------------
A driver file for the Queue class
Operations are:
Written by: Tyler Frye Tennessee Technological University
Written for: CSC 2110 Written on: March 05, 2010
---------------------------------------------------------------------------------*/
#include <iostream>
#include <iomanip>
using namespace std;
#include "Queue.h"
struct Teller_s {
bool active;
int time_At;
};
int main() {
char runAgain;
while (runAgain != 'N') {
int sim_Time, trans_Time, num_Serv, arriv_Time;
int i = 0, c_Time = 0; //Counters
int customers = 0, left, wait_Time = 0;
Queue bankQ;
cout << "\n------------------------------------------"
<< "\n- Welcome to the Bank Simulation Program -"
<< "\n------------------------------------------";
//Menu information
cout << "\n\nPlease input the following data(Time in minutes):\n";
cout << "\nLength of the Simulation: ";
cin >> sim_Time;
cout << "Average Transaction Time: ";
cin >> trans_Time;
cout << "Average Number of Servers: ";
cin >> num_Serv;
cout << "Average Time Between Arrivals: ";
cin >> arriv_Time;
Teller_s tellArray[num_Serv];
//Set all tellers to empty
for (i = 0; i < num_Serv; i++) {
tellArray[i].active = false;
tellArray[i].time_At = 0;
}
while (c_Time < sim_Time) {
if (c_Time % arriv_Time == 0) {
bankQ.enqueue();
customers++;
}
if (bankQ.front() != NULL) {
for (i = 0; i < num_Serv; i++) {
if (tellArray[i].active == false) {
bankQ.dequeue();
tellArray[i].active = true;
tellArray[i].time_At = trans_Time;
}
}
}
for (i = 0; i < num_Serv; i++) {
if (tellArray[i].active == true) {
tellArray[i].time_At--;
}
if (tellArray[i].time_At == 0 && tellArray[i].active == true) {
tellArray[i].active = false;
}
}
left = bankQ.getSize();
cout << endl << left;
wait_Time += left;
c_Time++;
}
cout << "\n---------------"
<< "\n- Data Output -"
<< "\n---------------\n";
cout << "Simulation Time: ";
cout << sim_Time << endl;
cout << "Average Transaction Time: ";
cout << trans_Time << endl;
cout << "Average Number of Servers: ";
cout << num_Serv << endl;
cout << "Average Time Between Arrivals: ";
cout << arriv_Time << endl << endl;
cout << "Average Total Wait Time: ";
cout << fixed << setprecision(2) << (float)wait_Time/customers;
cout << endl << wait_Time << endl;
cout << "\n\nRun the program again? (y/n): ";
cin >> runAgain;
runAgain = (char)toupper(runAgain);
while (runAgain != 'Y' && runAgain != 'N') {
cout << "Invalid Entry, please re-enter: ";
cin >> runAgain;
runAgain = (char)toupper(runAgain);
}
}
return 0;
}
/* ---LinkedList.h------------------------------------------------------------------
A header file for the Queue class
Operations are:
Written by: Tyler Frye Tennessee Technological University
Written for: CSC 2110 Written on: March 05, 2010
---------------------------------------------------------------------------------*/
#ifndef QUEUE
#define QUEUE
#include <iostream>
using namespace std;
typedef int ElementType;
class Queue {
public:
//Default constructor
Queue();
//Add to the back of the queue
void enqueue();
//Remove from the front of the queue
void dequeue();
//Returns the front of the queue
ElementType front();
//Return size of the queue
int getSize();
private:
class Node {
public:
ElementType data;
Node *next, *prev;
Node(ElementType i) { // Node constructor
data = i;
next = NULL;
prev = NULL;
}
}; //--- end of Node class
typedef Node *NodePointer;
Node *first;
Node *last;
}; //--- end of Queue class
//Operator overload statements here
#endif
/* ---Queue.cpp------------------------------------------------------------------
An implementation file for the Queue class
Operations are:
Written by: Tyler Frye Tennessee Technological University
Written for: CSC 2110 Written on: March 05, 2010
---------------------------------------------------------------------------------*/
#include <iostream>
using namespace std;
#include "Queue.h"
Queue::Queue() {
first = NULL;
last = NULL;
}
void Queue::enqueue() {
NodePointer nPtr = new Node(1);
NodePointer predPtr = first;
if (first == NULL) { //Insert if queue is empty
nPtr->next = first;
nPtr->prev = first;
first = nPtr;
} else {
while (predPtr->next != NULL) {
predPtr = predPtr->next;
}
nPtr->prev = predPtr;
nPtr->next = predPtr->next;
predPtr->next = nPtr;
}
last = nPtr; //Set last to new pointer
}
void Queue::dequeue() {
NodePointer dPtr = first;
first = first->next;
}
ElementType Queue::front() {
NodePointer ptr = first;
return ptr->data;
}
int Queue::getSize() {
int mySize = 0;
NodePointer ptr = first;
while (ptr != NULL) {
ptr = ptr->next;
mySize++;
}
return mySize;
}
Thanks!