Process exited with value 3221225477

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 "Process exited with value 3221225477" error and integer values get unreasonable values.
Where do i do wrong.
Can you help me. Thanks in advance
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#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;
}
Last edited on
if (!Q.empty() && Q.front().arrive_time + Q.front().service_duration <= currentTime) {
Hello, I think I have found your error.
First, Q is empty at first, and when the for loop begin, p's value is 0 or 1. As you say , with 0.5 random probability.But, if p is 0 at first, the second 'if' will execute, and in the condition part, Q.front() will execute , but you ought to note that Q is empty. You can't call front() to an empty queue. So, after I correct your code like this:
if (!Q.empty() && Q.front().arrive_time + Q.front().service_duration <= currentTime)

I run your code continuously and there is no error.I think that's correct.
L37 - Q may be empty.

This code looks like some C code mixed with a bit of C++. As C++, then possibly:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
#include <queue>
#include <iostream>
#include <random>
#include <iomanip>

std::mt19937 rng(std::random_device {}());

constexpr unsigned N { 30 };
constexpr unsigned MIN_SD { 1 };
constexpr unsigned MAX_SD { 5 };

struct Customer_Info {
	unsigned customer_number {};
	unsigned arrive_time {};
	unsigned service_duration {};
};

int main() {
	const std::uniform_int_distribution<unsigned> distrib(0, 1);
	const std::uniform_int_distribution<unsigned> dur(MIN_SD, MAX_SD);

	unsigned customernumber {};
	std::queue<Customer_Info> Q;

	std::cout << ("SIMULATION LOOP STARTED\n\n");

	for (unsigned currentTime {}; currentTime <= N; ++currentTime) {
		if (distrib(rng)) {
			Q.emplace(++customernumber, currentTime, dur(rng));
			std::cout << "TIME: " << std::setw(2) << currentTime << " Customer " << customernumber << " arrived\n";
		}

		//Check the front element of Queue to determine whether the front element is eligible to remove from Queue.
		if (!Q.empty() && (Q.front().arrive_time + Q.front().service_duration <= currentTime)) {
			std::cout << "TIME: " << std::setw(2) << currentTime << " Customer " << Q.front().customer_number << " departed. Service duration: " << Q.front().service_duration << '\n';
			Q.pop();
		}
	}

	std::cout << "\nSIMULATION LOOP FINISHED.";
	std::cout << "\n\nREMOVING REST OF THE CUSTOMERS FROM QUEUE.\n";

	while (!Q.empty()) {
		std::cout << "TIME: " << std::setw(2) << (Q.front().arrive_time + Q.front().service_duration) <<
			" Customer " << Q.front().customer_number << " departed. Service duration: " << Q.front().service_duration << '\n';
		Q.pop();
	}
}

1. C++ has the ability to generate pseudo random numbers, so no need to use the C functions.
https://en.cppreference.com/w/cpp/numeric/random

2. C++20 added a printf-like feature, std::format, so no need to use C functions in C++ code.*
https://en.cppreference.com/w/cpp/utility/format

3. using #define to create constant variables is not recommended for C++ code, use const (or constexpr).
https://stackoverflow.com/questions/6442328/what-is-the-difference-between-define-and-const

Just for gits and shiggles I refactored the code, using what C++ has to offer:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
#include <iostream>
#include <queue>
#include <random>
#include <chrono>

struct Customer_Info
{
   int customer_number;
   int arrive_time;
   int service_duration;
};

int main()
{
   const int N { 30 };
   const int MIN_SD { 1 };
   const int MAX_SD { 5 };

   std::default_random_engine prng { std::random_device {} () };

   std::uniform_int_distribution<int> dis1(0, 1);
   std::uniform_int_distribution<int> dis2(MIN_SD, MAX_SD);

   Customer_Info CustomerInfo;

   int customernumber { };

   std::queue<Customer_Info> Q;

   std::cout << "SIMULATION LOOP STARTED\n\n";

   for (int currentTime { }; currentTime <= N; currentTime++)
   {
      //with 0.5 random probability
      int p { dis1(prng) };

      if (p)
      { //If there is customer arrival
         CustomerInfo.customer_number = ++customernumber;
         CustomerInfo.service_duration = dis2(prng);
         CustomerInfo.arrive_time = currentTime;

         std::cout << std::format("TIME: {:2} Customer {:2} 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.empty() && Q.front().arrive_time + Q.front().service_duration <= currentTime)
      {
         std::cout << std::format("TIME: {:2} Customer {:2} departed. Service duration:{}\n",
                                  currentTime, Q.front().customer_number, Q.front().service_duration);
         Q.pop();
      }
   }

   std::cout << "\nSIMULATION LOOP FINISHED.";

   std::cout << "\n\nREMOVING REST OF THE CUSTOMERS FROM QUEUE.\n";

   while (!Q.empty())
   {
      std::cout << std::format("TIME: {:2} Customer {:2} departed. Service duration: {}\n",
                               (Q.front().arrive_time + Q.front().service_duration), Q.front().customer_number, Q.front().service_duration);

      Q.pop();
   }
}
SIMULATION LOOP STARTED

TIME:  1 Customer  1 arrived.
TIME:  2 Customer  2 arrived.
TIME:  3 Customer  1 departed. Service duration:2
TIME:  4 Customer  3 arrived.
TIME:  7 Customer  2 departed. Service duration:5
TIME:  8 Customer  4 arrived.
TIME:  8 Customer  3 departed. Service duration:4
TIME: 10 Customer  5 arrived.
TIME: 13 Customer  4 departed. Service duration:5
TIME: 15 Customer  6 arrived.
TIME: 15 Customer  5 departed. Service duration:5
TIME: 18 Customer  6 departed. Service duration:3
TIME: 22 Customer  7 arrived.
TIME: 24 Customer  7 departed. Service duration:2
TIME: 25 Customer  8 arrived.
TIME: 27 Customer  9 arrived.
TIME: 29 Customer 10 arrived.
TIME: 29 Customer  8 departed. Service duration:4
TIME: 30 Customer  9 departed. Service duration:1

SIMULATION LOOP FINISHED.

REMOVING REST OF THE CUSTOMERS FROM QUEUE.
TIME: 34 Customer 10 departed. Service duration: 5


*std::format requires C++20, everything else is C++11 or later. If your compiler isn't C++20 compliant yet, only Visual Studio is currently, you could use the {fmt} 3rd party library.
https://fmt.dev/latest/index.html
Last edited on
If you use Q.emplace() instead of Q.push(), then you don't need CustomerInfo as per my code above...

Thank you all. That helps a lot.
Topic archived. No new replies allowed.