michaelScott
Junior Member level 2
Hello friends,
I am currently working on a homework. But the code i wrote is not works as expected what i mean is that when i run the code sometimes i get the following error and integer values get unreasonable values.
Can you help me. Thanks in advance
I am currently working on a homework. But the code i wrote is not works as expected what i mean is that when i run the code sometimes i get the following error and integer values get unreasonable values.
Where do i do wrong."Process exited with value 3221225477"
Can you help me. Thanks in advance
C++:
#include <stdio.h>
#include <stdlib.h>
#include <queue>
#include <cstdlib> //contains prototypes for functions srand and rand
using std::srand;
using std::rand;
#include <ctime> //contains prototypes for function time
using std::time;
using namespace std;
#define N 30
#define MIN_SD 1
#define MAX_SD 5
struct Customer_Info {
int customer_number;
int arrive_time;
int service_duration;
};
struct Customer_Info CustomerInfo;
int main() {
int customernumber = 0;
queue<Customer_Info> Q;
srand(time(NULL));
printf("SIMULATION LOOP STARTED\n\n");
for (int currentTime = 0; currentTime <= N; currentTime++) {
//with 0.5 random probability
int p = rand() % 2;
if (p) { //If there is customer arrival
CustomerInfo.customer_number = ++customernumber;
CustomerInfo.service_duration = (rand()%MAX_SD) + MIN_SD;
CustomerInfo.arrive_time = currentTime;
printf("TIME:%02d Customer %d arrived.\n", currentTime, CustomerInfo.customer_number);
Q.push(CustomerInfo); // Add (enqueue) structure to queue
}
//Check the front element of Queue to determine whether the front element is eligible to remove from Queue.
if (Q.front().arrive_time + Q.front().service_duration <= currentTime) {
printf("TIME:%02d Customer %d departed. Service duration:%d\n", currentTime, Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
}
printf("\nSIMULATION LOOP FINISHED.");
printf("\n\nREMOVING REST OF THE CUSTOMERS FROM QUEUE.\n");
while (!Q.empty()) {
printf("TIME: %2d Customer %2d departed. Service duration: %d\n", (Q.front().arrive_time + Q.front().service_duration), Q.front().customer_number, Q.front().service_duration);
Q.pop();
}
//system("pause");
return 0;
}