help

I wrote this code
to validate the time input
if there is a free slot slots array
then the time option will appear
but the code does not work properly
what is the problem??
int slotoptions[]={10,11,12};
bool slots[]={true,true,true};
cout<<"please enter the time: ";
cin>>time;
for (int i = 0; i < 3; i++)
{
if(slots[i]==true)
{
if(time==slotoptions[i])
{
a[n].time=time;
slots[i]=false;break;
}
}
if(slots[i]==false)
{
cout<<"Sorry, this is not available \n";
cout<<"please enter the time: ";
cin>>time;
}
}
Maybe you want something like 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
int slotoptions[]={10,11,12};
bool slots[]={true,true,true};
bool available = false;
bool ok = false;
while(! available)
{
  cout<<"please enter the time: ";
  cin>>time;
  for (int i = 0; i < 3; i++)
  {
    available = (time==slotoptions[i]);
    if(available)
    {
      if(slots[i])
      {
        ok = true;
        a[i].time=time;
        slots[i]=false;
        break;
      }
    }
  }
  if(available)
  {
    if(ok)
      cout<<"Success\n";
    else
      cout<<"No slot available\n";
  }
  else
      cout<<"Invalid time\n";
}
Not tested!
Topic archived. No new replies allowed.