# Thread: Array Problem

1. ## Array Problem

hi

how can i revise the code below so that as well as being able to book a particular hour in the day, I can also request the first free hour of the day?

thanks

#include <iostream.h>
#include <string.h>
#include<conio.h>

struct book
{
char time[5];
bool status;
};

//the array
void makebook(book z[])
{
strcpy(z[0].time,"0900");
strcpy(z[1].time,"1000");
strcpy(z[2].time,"1100");
strcpy(z[3].time,"1200");
strcpy(z[4].time,"1300");
strcpy(z[5].time,"1400");
strcpy(z[6].time,"1500");
strcpy(z[7].time,"1600");
strcpy(z[8].time,"1700");

z[0].status=true;
z[1].status=true;
z[2].status=true;
z[3].status=true;
z[4].status=true;
z[5].status=true;
z[6].status=true;
z[7].status=true;
z[8].status=true;
}

void printbook(book z[])
{
for(int i=0;i<9;i++)
{
cout<<i+1<<" "<<z[i].time<<"\t";

if(z[i].status!=true)
cout<<"Booked"<<endl;
else
cout<<""<<endl;
}
}

void main ()
{
book ob[9];
makebook(ob);

printbook(ob);

char date[11];

cout<<"enter the date in dd/mm/yy format : ";
cin>>date;

{
cout<<"Enter number of hour to enter appointment : ";

int num;
cin>>num;
num = num - 1;

//checks to see if time already booked
if(ob[num].status==false)
cout<<ob[num].time<<" is already booked"<<endl;
else
{
cout<<ob[num].time<<" is now booked "<<endl;
ob[num].status=false;
}
cout<<"Enter another appointment ? (y/n) : ";
char temp;
cin>>temp;
if(temp=='n')
}
// display output on monitor
cout<<"\nAppointments for "<<date<<endl;
cout<<"\n";
printbook(ob);
cout<<"\n";
}

2. It looks like you need to make use of the FOR loop. Not only will this make initialization much cleaner, but you can also use it to detect for an unbooked hour.

// initialization
for( int i = 0; i < 8; i++ )
x[i].status = true;

I think you've got enough skill to figure out how to check for the first unbooked hour if you use FOR.

Popular pages Recent additions