For C++ programming So I am having trouble here on this code everytime that I ru
ID: 3701387 • Letter: F
Question
For C++ programming
So I am having trouble here on this code everytime that I run it it either works 1 out of 10 times or not at all I keep on getting this error:
Exception thrown: read access violation.
temp was nullptr.
Thanks:)
==========
Here is the code
========
#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;
}
Explanation / Answer
void incrementWaitTime(){
Customer *temp;
temp = top;
if(top == NULL){
return;
}
else{
if(temp->next == bottom){
temp->WaitTime++;
}
else{
while(temp->next !=NULL && temp->next != bottom ){
temp->WaitTime = temp->WaitTime + 1;
temp = temp->next;
}
}
}
}
/* there is issue with function void incrementWaitTime()
while(temp->next != bottom) is modified to
while(temp->next !=NULL && temp->next != bottom )
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.