Hi. I need help with my queue project.

I wrote a queue program that is supposed to give the following outputs after the first 30 minutes.
The time of arrival for each customer.
The time of leaving for each customer.
How many customers are in the line.
Who is the current serving customer.

The program is almost working, a couple of the outputs are incorrect.
Can you help me?
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;

int main()
{
	int customer;
	queue<int> que;

	for (int i = 0; i < 30; i++)
	{
		if(i += 3)
		{
			que.push(customer);
			cout<<"At "<<i<<" a customer has arrived."<<endl;
		}
		if(i += 5)
		{
			que.pop();
			cout<<"At "<<i<<" a customer has left."<<endl;
		}
		}

		cout<<"There are "<<que.size()<<" customers in line."<<endl;

		cout<<"Customer number "<<customer<<" is the current serving customer."<<endl;

		return 0;
}
Your useage of "i += 3" and "i += 5" in the if statements changes the value of i. Additionally the if statements will always be true since i is always above 0 in the conditions.

I think you meant to use & instead of +=, like this:
1
2
3
4
if(i % 3 == 0)
{
    //...
}


Also, lines 22-28 are indented one tab too far.
I did that and I changed the for loop to
 
for (int i = 1; i < 30; i++)

Now the first 2 task are working. I just need to work on the other 2.
Thanks.
Could you post your updated 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
#include<iostream>
#include<queue>
using namespace std;

int main()
{
	int customer;
	queue<int> que;

	for (int i = 1; i < 30; i++)
	{
		if(i % 3 == 0)
		{
			que.push(customer);
			cout<<"At "<<i<<" a customer has arrived."<<endl;
		}
		if(i % 5 == 0)
		{
			que.pop();
			cout<<"At "<<i<<" a customer has left."<<endl;
		}
		}

		cout<<"There are "<<que.size()<<" customers in line."<<endl;

		cout<<"Customer number "<<customer<<" is the current serving customer."<<endl;

		return 0;
}
You never initialize or change the value of customer, what is this variable even for?
Topic archived. No new replies allowed.