I'm creating a restaurant simulation for my data structures class using a queue, and have a good idea for how to do this, but my program keeps throwing an error and marking my function that increments the "wait time" variable of all "customers" in the queue.
Sometimes my program will run all the way through and generate a value for the average wait time, and other times it will break after the first or second customer is generated. I thought that my conditionals that I made my incrementWaitTime
function with would prevent it from reading from a NULL memory location, but apparently they haven't. Could someone glance over my source code and help me figure out what's broken?
Edit: Here's the exact error that my program is throwing:
"Unhandled exception at 0x010419aa in Homework 2 Restaurant Queue.exe: 0xC0000005: Access violation reading location 0x00000018."
#include <iostream>
#include <time.h>
#include <stdlib.h>
using namespace std;
class Customer{
public:
int WaitTime, ServiceTime, OrderCounter, ServiceCounter, OrderTime, test;
Customer *next;
Customer(){
test = 0;
OrderCounter = 0;
WaitTime = 0;
ServiceTime = 0;
OrderTime = 0;
next = NULL;
}
};
class Queue{
public:
Customer *top, *bottom;
int totalWaitTime, totalCustomers;
Queue(){
bottom = NULL;
top = NULL;
totalWaitTime = 0;
totalCustomers = 0;
}
void incrementWaitTime(){
Customer *temp;
temp = top;
if(top == NULL){
return;
}
else{
if(temp->next == bottom){
temp->WaitTime++;
}
else{
while(temp->next != bottom){
temp->WaitTime = temp->WaitTime + 1;
temp = temp->next;
}
}
}
}
void displayContents(){
Customer *temp;
temp = top;
while(temp!= NULL){
cout << temp->test << "---->";
temp = temp->next;
}
cout << endl;
}
void newCustomer(int x){
Customer *temp = new Customer;
temp->OrderTime = x;
if(top == NULL){ //No customers in line
top = bottom = temp;
totalCustomers++;
cout << "There is a new customer." << endl;
}
else{
temp->next = top;
top = temp;
totalCustomers++;
cout << "There is a new customer." << endl;
}
}
void removeCustomer(){
Customer *chase, *follow;
chase = follow = top;
if(top == NULL){
//No customers in queue.
cout << "No customers are in line.. there's nothing to remove." << endl;
return;
}
else{
if(top->next == NULL){
//Only one customer
delete top;
top = NULL;
bottom = NULL;
return;
}
while(chase->next != NULL){
follow = chase;
chase = chase->next;
}
delete chase;
bottom = follow;
follow->next = NULL;
}
}
void checkStatus(){
if(top == NULL){
bottom = NULL;
return;
}
else if(bottom->OrderCounter != bottom->OrderTime){
bottom->OrderCounter++;
bottom->WaitTime++;
}
else{
totalWaitTime = totalWaitTime + bottom->WaitTime;
removeCustomer();
}
}
};
int main(){
Queue Restaurant;
int Clock = 1, totalCustomers = 0;
float probArrival = 0;
srand(time(NULL)); //seeds random number generator
int number, orderTime, TotalWaitTime, TotalServiceTime;
while(Clock < 1140){
while(Clock < 120){
number = rand();
number = number%10+1; //Generates a number between 1 and 10
if(number >=1 && number <= 3){
orderTime = rand();
orderTime = orderTime%6 + 1; //Creates orderTime between 1 and 6
Restaurant.newCustomer(orderTime);
cout << "The time is: " << Clock << " minutes." << endl;
Clock++;
}
else{
Restaurant.checkStatus();
Restaurant.incrementWaitTime();
cout << "The time is: " << Clock << " minutes." << endl;
Clock++;
}
}
Clock = 1140;
}
cout << "There were " << Restaurant.totalCustomers << " customers. " << endl;
cout << "Average wait time was: " << Restaurant.totalWaitTime / Restaurant.totalCustomers << " minutes per customer." << endl;
system("pause");
return 1;
}
User contributions licensed under CC BY-SA 3.0