need help passing a queue by refrence

so i realized my pass2queue function had the queue being reset each time it left it... i realized i needed to pass my queue into everything in order for it to work. im having a bit of trouble on how to pass my queue so it wont delete it every time im trying to store somthing into it so that way i can print everything in it at the end. my program right now it just reseting the queue each time it goes into there i was trying to add & before myqueue and didnt have much luck here is my code so far any help would be great

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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<iostream>
#include <queue>
#include <vector>
#include <stdlib.h>
#include <time.h>
using namespace std;

void createjobs(int time, queue<int>myqueue);
void addjobtime5(int time, queue<int>myqueue);
void addjobtime10(int time, queue<int> myqueue);
void addjobtime25(int time, queue<int> myqueue );
void addjobtime30(int time, queue<int> myqueue);
void pass2queue(int processtime, int priority, int endtime, queue<int> myqueue);
void printqueue(queue<int> &myqueue);
int main()
{
	priority_queue<int> pqueue;
	queue<int> myqueue;
//	priority_queue<int>test = myqueue();
	createjobs(1, myqueue);
	srand(time(0));
	system("pause");
	return 0;
}



void createjobs(int start, queue<int>myqueue)
{
	int totalj = 0;
	int totalj1 = 0;
	int totalj2 = 0;
	int totalj3 = 0;
	int totalj4 = 0;
	int randnum=0;
	int totaltime = 0;
	int endtime = 500;//choose how many time units you want to run
	for (int time = 1; 0 < endtime; time++)
	{ 
		
		cout << "THE TIME: " << time << endl;
		totaltime = time;
		int temp1 = 4, temp2 = 9, temp3 = 24, temp4 = 25;
		if (time % 5 == 0 && time % 10 == 0 && time % 25 == 0 && time % 30==0)
		{
			//cout << "job 4 time " << time << endl;
			
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime, myqueue);
			totalj1++;
			totalj++;
			
			
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10: " << temp2 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime10(endtime,myqueue);
			totalj2++;
			totalj++;
			
			
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25: " << temp3 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime25(endtime,myqueue);
			totalj++;
			totalj3++;
			
			
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30: " << temp4 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime30(endtime,myqueue);
			totalj++;
			totalj4++;
			
		}
		else if (time % 5 == 0 && time % 10 == 0 && time % 30 == 0)
		{
			//cout << "job 6 time " << time << endl;
			
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;
			
			
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10: " << temp2 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime10(endtime,myqueue);
			totalj++;
			totalj2++;
			
			
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30: " << temp4 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime30(endtime,myqueue);
			totalj++;
			totalj4++;
			
		}
		
2nd half of code
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
else if (time % 5 == 0 && time % 25 == 0 && time % 30==0)
		{
			//cout << "job 7 time " << time << endl;
			
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;
			
			
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25: " << temp3 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime25(endtime,myqueue);
			totalj++;
			totalj3++;
			
			
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30: " << temp4 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime30(endtime,myqueue);
			totalj++;
			totalj4++;
			
		}
		else if (time % 5 == 0 && time % 30==0)
		{
			//cout << "job 8 time " << time << endl;
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;
			
	
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30: " << temp4 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime30(endtime,myqueue);
			totalj++;
			totalj4++;
			
		}
		else if (time % 5 == 0 && time % 25==0)
		{
			//cout << "job 12 time " << time << endl;
			
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;

			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25: " << temp3 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime25(endtime,myqueue);
			totalj++;
			totalj3++;
			
		}

		else if (time % 5 == 0 && time % 10==0)
		{
			//cout << "job 13 time " << time << endl;
		
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;
			
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10: " << temp2 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime10(endtime,myqueue);
			totalj++;
			totalj2++;
			
		}
		else if (time % 10 == 0 && time % 30==0)
		{
			//cout << "job 9 time " << time << endl;
			
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10: " << temp2 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime10(endtime,myqueue);
			totalj++;
			totalj2++;
			
			
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30: " << temp4 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime30(endtime,myqueue);
			totalj++;
			totalj4++;
			
		}
		else if (time % 10 == 0)
		{
			//cout << "job 16 time " << time << endl;
			
			randnum = rand() % (3);
			temp2 = temp2 + randnum;
			endtime = endtime - temp2;
			cout << "job 10: " << temp2 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime10(endtime,myqueue);
			totalj++;
			totalj2++;
			
		}
		else if (time % 25 == 0)
		{
			//cout << "job 11 time " << time << endl;
			
			randnum = rand() % (3);
			temp3 = temp3 + randnum;
			endtime = endtime - temp3;
			cout << "job 25: " << temp3 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime25(endtime,myqueue);
			totalj++;
			totalj3++;
			
		}
		else if (time % 5 == 0)
		{
			//cout << "job 18 time " << time << endl;
			randnum = rand() % (3);
			temp1 = temp1 + randnum;
			endtime = endtime - temp1;
			cout << "job 5: " << temp1 << " random number: " << randnum << " time remaining: " << endtime << endl;
			addjobtime5(endtime,myqueue);
			totalj1++;
			totalj++;
			
		}
		else if (time % 30 == 0)
		{
			//cout << "job 122 time " << time << endl;
			
			randnum = rand() % (11);
			temp4 = temp4 + randnum;
			endtime = endtime - temp4;
			cout << "job 30 " << temp4 << "  randy " << randnum << "end " << endtime << endl;
			addjobtime30(endtime, myqueue);
			totalj++;
			totalj4++;
			
		}
		else
		{
			//cout << "no jobs created" << endl;
		
		}
	}
	cout << endl;
	cout << "Metrics" << endl;
	cout << "------------------" << endl;
	cout << "total job 1: " << totalj1 << endl;
	cout << "total job 2: " << totalj2 << endl;
	cout << "total job 3: " << totalj3<< endl;
	cout << "total job 4: " << totalj4 << endl;
	cout << "total jobs: " << totalj << endl;
	cout << "time taken for job arrivals to equal 9500 time units: " << totaltime << endl;
}

void addjobtime5(int endtime, queue<int>myqueue)
{
	int holder = endtime;
	//cout<<"THE END TIME: " << holder << endl;
	int process5time = 1;
	int randnum = rand() % (6);
	process5time = process5time + randnum;
	cout << "processing time for job 1: " << process5time << "= 1 + " << randnum << endl;
	pass2queue(process5time, 2, holder, myqueue);
	
	//cout << "job 5 time YO ITS DIVISIBLE BY 5->" << time << endl;
}

void addjobtime10(int endtime, queue<int>myqueue)
{
	int holder = endtime;
	//cout << "THE END TIME: " << holder << endl;
	int process10time = 6;
	int randnum = rand() % (6);
	process10time = process10time + randnum;
	cout << "processing time for job 2: " << process10time << "= 6 + " << randnum << endl;
	pass2queue(process10time, 2, holder, myqueue);
	
	//cout << "job 10 time YO ITS DIVISIBLE BY 10 -> " << time << endl;
}

void addjobtime25(int endtime, queue<int>myqueue)
{
	int holder = endtime;
	//cout << "THE END TIME: " << holder << endl;
	int process25time = 11;
	int randnum = rand() % (6);
	process25time = process25time + randnum;
	cout << "processing time for job 3: " << process25time << "= 11 + " << randnum << endl;
	pass2queue(process25time, 2, holder,myqueue);

	//cout << "job 25 time YO ITS DIVISIBLE BY 25->" << time << endl;
}
void addjobtime30(int endtime, queue<int>myqueue)
{
	int holder = endtime;
	//cout << "THE END TIME: " << holder << endl;
	int process30time = 8;
	int randnum = rand() % (6);
	process30time = process30time + randnum;
	cout << "processing time for job 4:  " << process30time << "= 8 + " << randnum << endl;
	pass2queue(process30time, 1, holder,myqueue);
	
	//cout << "job 30 time YO ITS DIVISIBLE BY 30->" << time << endl;
}

void pass2queue(int processtime, int priority, int endtime, queue<int>myqueue)
{
	int holder = endtime;
	int protime = processtime;
	int x = 0;
	//cout << " THE END TIME " << holder << " pro time " << protime << endl;
	if (holder >= 0)
	{
		cout << "stored " << protime << " in queue " << endl;
		myqueue.push(protime);
	}
	else if(holder < 0)
	{
		while (!myqueue.empty())
		{
			x = myqueue.front();
			cout << x << " top of queue" << endl;
			myqueue.pop();
			
		}
	}
}
It looks like you're only passing the queue by reference in the print function. Wherever you want things to use the same queue you'd need to pass it by reference to every function call, i.e. pass by reference in all 7 functions shown.

You could also form a class with all of your functions and have the queue be a variable within the class. In your main you could declare a class variable. This way, when you want to call your create function you'd use the class variable name, a period, followed by the function name with parameters, then there would be no need to pass the queue.
Topic archived. No new replies allowed.