Queue refusing to take a string!

Hi.
The
X
in the push function is reflecting an error that I don't understand.
And in the nested for loop in the int main, you can see that I am trying to tell the program that when
i
is incremented three times, it should push a new name(string)

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
#include<iostream>
#include<string>
using namespace std;
class queue {
public:
	void queue_init(int size);
	void push(string x);
	void pop();
	int size();
	char front();
	char back();
	int Empty();
	int full();


private:
	char* queue_ptr;
	int max_len;
	int rear_cnt;
	int front_cnt;
};

int queue::size() {
	return (rear_cnt - front_cnt);
}

void queue::queue_init(int arr_size) {
	queue_ptr = new char[arr_size];
	max_len = arr_size - 1;
	front_cnt = 0;
	rear_cnt = 0;
}

int queue::Empty() {
	return (rear_cnt == front_cnt);
}

int queue::full() {
	return (rear_cnt == max_len + 1);
}
char queue::front() {
	return (queue_ptr[front_cnt]);
}

char queue::back() {
	return (queue_ptr[rear_cnt - 1]);
}

void queue::push(string X) {
	if (rear_cnt == max_len + 1)
		std::cout << "Error queue is full \n";
	else
	{
		queue_ptr[rear_cnt] = X;


		rear_cnt++;
	}
}

void queue::pop() {

	if (Empty())
		std::cout << "Error queue is empty\n";
	else
	{
		queue_ptr[front_cnt] = 0;
		front_cnt++;
	}
}



int main(){
	queue Q;
	Q.queue_init(10);
	const int array_size = 11;
	string customer[array_size] = {"George", "Ahmed", "Abdullah", "Jonah", "Lamar", "Ronan", "Ali", "Peanuts", "James", "Omar", "Yassir"};
	std::cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
		"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
	std::cout << "The customer service opens at 8:10 am" << endl;
	for (int i = 0; i <= 30; i++) {
		for (int j = 0; j <= (i + 3); j++){
			Q.push(customer[array_size]);
			std::cout << "There are: "; Q.size(); std::cout << "waiting on line currently" << endl;
		}
	}
	return 0;
}
Last edited on
The reason you get an error message on line 54 is because you are trying to assign a std::string to a char.

queue_ptr is a pointer to a char. If you want to store std::strings, shouldn't it be a pointer to a std::string?
peter87

Woah, Thanks!
I think I just fixed it-ish.
Moreover, the code doesn't give me an error, however in the black screen, it crashes.
I think it crashes at the first
push function
.
(this is another similar version but with different for loop content)

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
#include<iostream>
#include<string>
using namespace std;
class queue {
public:
	void queue_init(int size);
	void push(string x);
	void pop();
	int size();
	string front();
	string back();
	int Empty();
	int full();


private:
	string* queue_ptr;
	int max_len;
	int rear_cnt;
	int front_cnt;
};

int queue::size() {
	return (rear_cnt - front_cnt);
}

void queue::queue_init(int arr_size) {
	queue_ptr = new string[arr_size];
	max_len = arr_size - 1;
	front_cnt = 0;
	rear_cnt = 0;
}

int queue::Empty() {
	return (rear_cnt == front_cnt);
}

int queue::full() {
	return (rear_cnt == max_len + 1);
}
string queue::front() {
	return (queue_ptr[front_cnt]);
}

string queue::back() {
	return (queue_ptr[rear_cnt - 1]);
}

void queue::push(string X) {
	if (rear_cnt == max_len + 1)
		std::cout << "Error queue is full \n";
	else
	{
		queue_ptr[rear_cnt] = X;


		rear_cnt++;
	}
}

void queue::pop() {

	if (Empty())
		std::cout << "Error queue is empty\n";
	else
	{
		queue_ptr[front_cnt] = "";
		front_cnt++;
	}
}



int main(){
	queue Q;
	Q.queue_init(10);
	const int array_size = 11;
	string customer[array_size] = { "George", "Ahmed", "Abdullah", "Jonah", "Lamar", "Ronan", "Ali", "Peanuts", "James", "Omar", "Yassir" };
	std::cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
		"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
	std::cout << "The customer service opens at 8:10 am" << endl;
	for (int i = 0; i <= 30; i++)
	{
		if (i == 0) {
			cout << "it is 8:10 am. The first customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 3) {
			cout << "it is 8:13 am. The second customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently." << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;

		}
		if (i == 5){
			cout << "it is 8: 15 am. The first customer is served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}

		if (i == 6){
			cout << "it is 8: 16 am. The third customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 9){
			cout << "it is 8: 19 am. The fourth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 10){
			cout << "it is 8: 20 am. The second customer is served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 12){
			cout << "it is 8: 22 am. The fifth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 15){
			cout << "it is 8: 25 am. The sixth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "and the third customer just got served."; Q.pop(); cout << endl;
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 18){
			cout << "it is 8: 28 am. The seventh customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 20){
			cout << "it is 8: 30 am. The fourth customer just got served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 21){
			cout << "it is 8: 31 am. The eighth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 24){
			cout << "it is 8: 34 am. The ninth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 25){
			cout << "it is 8: 35 am. The fifth customer just got served." << endl;
			Q.pop();
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 27){
			cout << "it is 8: 37 am. The tenth customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
		if (i == 30){
			cout << "it is 8: 40 am. The eleventh customer just arrived." << endl;
			Q.push(customer[array_size]);
			cout << "and the sixth customer just got served."; Q.pop(); cout << endl;
			cout << "There are: "; Q.size(); cout << "waiting on line currently" << endl;
			cout << "currently serving: "; Q.front(); cout << endl;
			cout << " ************************************************************************* " << endl;
		}
	}
	return 0;
}
array_size is not a valid index for customer.
what do you suggest I put?

Thanks,
Abdul
I suggest you replace array_size by the index of the element that you want to push.
Ok, so this is another try in a different way too.

Without using array, and now I am using recursive style in that every time it
pushes an element
i
jumps three steps forward and pushes another element.
now it it not showing any queue action really.
it is not pushing nor popping.

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
#include<iostream>
#include<queue>
using namespace std;

void recursive(int n) {
	while (n < 30)
	{
		n + 3;
	}
}
int main(){
	queue<char> Q;
	char a = 'a', b = 'b', c = 'c', d = 'd', e = 'e', f = 'f', g = 'g', h = 'h', j = 'j', k = 'k', l = 'l';
	cout << "This program will calculate the customer service throughout the next 30 minutes.\n"
		"Since a customer enters Rajhi Bank every 3 minutes, therefore the size of the queue should be 10 or a possible 11." << endl;
	cout << "The customer service opens at 8:10 am" << endl;
	for (int i = 1; i <= 30; i++){
		while (i < 30){
			Q.push(a);
			recursive(i);
		}
		if (i % 5){
			Q.pop();
		}
		Q.size();
		Q.front();
	}
		return 0;
}
1) That's not what "recursive" means.

2) Your recursive() function achieves nothing at all. You're passing n by value, which means that the function has its own copy of that variable. The function changes the value of its own copy. It doesn't change the value of the variable in the calling code. To do that, you need to pass the value by reference.

Even if you do that, the logic doesn't make sense. All that will happen is that the recursive() function will change the value to a number that's bigger than 29 the first time you call it, and the loop at lines 18-21 will exit after the first iteration. So you'll only push a single character onto your queue.

I recommend that you learn how to use a debugger to step through the code. That way, you'll be able to see for yourself what the code is actually doing, and understand why it's not doing what you intend.
Last edited on
You have an infinite loop, also your recursive function isn't recursive and does nothing.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include <iostream>
#include <queue>
using namespace std;

int main(){
	queue<int> Q;
	
	for (int i = 1; i <= 30; i++){
		if(i % 3 == 0)
		{
			Q.push(i);
			cout << Q.size();
			cout << " " << Q.back() << endl;
		}
	}
}
naraku9333

I can see that it is infinite, why is that happening and how may I stop it?

Thanks,
Abdul
My post explains why it is happening, and what change would stop it. It also tells you that, even if you make that change, the logic of your program doesn't make sense.
Thanks for the quick response MikeyBoy.

The logic of the program is that in the bank, for the next 30 minutes, a customer arrives every 3 minutes. and it takes 5 minutes to finish serving a customer.
Topic archived. No new replies allowed.