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?
#include<iostream>
#include<queue>
usingnamespace 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:
#include<iostream>
#include<queue>
usingnamespace 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;
}