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)
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)
#include<iostream>
#include<queue>
usingnamespace 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;
}
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.
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.
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.