I am making a larger program that has the following part:
I will avoid putting the larger one and work out how the solution to this problem applies to that one.
Here, I have four tables/counters who count the number of customers in line.
I hope the comments help to understand.
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
|
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a=0,b=0,c=0,d=0; //The four counters
int flag[3]; //The array that keeps track whether they are open or closed
int full[3]; //The array that keeps track whether they are full yet. (Full at 10)
flag[0]=1; //Initially 2 counters are open
flag[1]=1;
for(int m=0;m<=3;m++) //Initially all counters are not full
{
full[m]=0;
}
for(;;)
{
char name;
cout<<"Enter counter name: \n";
cin>>name;
switch(name)
{//this code is given below}
cout<<"\n The tally: \n "; //This part is just for reference or debugging so you guys understand the output
cout<<" \t a \t "<<a<<endl; //I don't need to display output in my original program
cout<<" \t b \t "<<b<<endl;
cout<<" \t c \t "<<c<<endl;
cout<<" \t d \t "<<d<<endl;
}
//The activator code goes here
| |
What I need to do is open the third counter 'c' when either of the initial counters are full. What I did in the switch case was this:
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
|
switch(name){
case 'a':
{
if(flag[0]==1) //Only if counter is open
{
a++;
if(a==10)
{
flag[0]=0; //Close counter
full[0]=1; //Counter is full
}
}
else //If counter is closed
{
cout<<"Counter closed. \n ";
}
break;
}
case 'b':
{
if(flag[1]==1) //Only if counter is open
{
b++;
if(1==10)
{
flag[1]=0;
full[1]=1;
}
}
else
{
cout<<"Counter closed. ";
}
break;
}
case 'c':
{
if(flag[2]==1) //Only if counter is open
{
c++;
if(c==10)
{
flag[2]=0;
full[2]=1;
}
}
else
{
cout<<"Counter closed. ";
}
break;
}
case 'd':
{
if(flag[3]==1) //Only if counter is open
{
d++;
if(d==10)
{
flag[3]=0;
full[3]=1;
}
}
else
{
cout<<"Counter closed. ";
}
break;
}
}
| |
So the flag for any counter goes 0 as soon as it becomes full.
So I figured I could check all the counters if one of them is full, and open a counter which is not full.
so, the activator code given above is like,
1 2 3 4
|
if(full[0]==1 || full[1]==1)
{
flag[2]==1;
}
| |
Also, I need to check if 2 out of 3 counters are full and open another one which is not full.
If 3 out of 4 counters are full, do nothing.
How do I make it a bit more smooth? Say I have like 100 counters, I need to check everyone to see if say, 'n' are full, then search for the first unopened counter and open it.
An interesting twist is when I included decrementers (in my larger program), so now the counters can decrease and go back to zero.
example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
case 'a':
{
if(a!=0) //Only if counter is not empty
{
--a;
if(a==0)
{
flag[0]=0; //Close counter
full[0]=0; //Counter is empty
}
}
else //If counter is closed
{
cout<<"Counter closed. \n ";
}
break;
| |
So now, let's say at some point int the program, counters 'b' and 'c' are open, and 'b' gets full, I want to open 'a' again, not 'd'.
How do I do THAT?