:confused:
hi everyone as much involvement i get with C++ i alway get to a point where my fingers are tied, and here i am.
This piece of code displays a date then the time selected by the user, wherei got stuck are:
1. i tried to validate (check) the date enter so that it conforms to something like this 18 03 2006.
2. i tried to make it allow a user to request the first free hour of the day.
#include "stdafx.h"
#using <mscorlib.dll>
using namespace std;
class Day{
private:
int day,month,year;//Date
bool schedule[9];//Hours 9am-6pm
public:
Day(int,int,int);//Constructor
~Day();//Destructor
void setTime(int);
bool checkTime(int);
void checkDate(int,int,int);
//==========================
void firstAvailableHour();// Get the first avalaiable hour in the appointment book
void display();
bool IfAnyTimeAvailable()
{
for(int i=0;i<9;++i) {
if(!schedule[i]) {
return true ;//time is available for booking
}
}
return false ;//no time is available for booking
}
};
Day::Day(int d,int m,int y){//Pass date to constructor
day=d;month=m;year=y;//Set the date
for(int i=0;i<9;++i)schedule[i]=false;//Initialise all the hours in the day to “free
}
Day::~Day(){}
bool Day::checkTime(int t)
{
if(t>5&&t<9)return 0;//Invalid time
else t+=(t>=9?-9:3);
if(!schedule[t])
{//Check if booked
schedule[t]=true;//Now booked
return 1;//Success!
}
else return 0;//Not available...
}
void Day::checkDate(int d,int m,int y);
if (d <= 31 && d >= 1 && m <= 12 && m >= 1 && y <=2006)
{
clrscr();
cout<<" Date is kindly Accepted";
// day = d;
//month = m;
//year = y;
cout<< "wait \n";
Sleep(2000); // go to bed for 2secs.
}
else
{
cout<<"date is invalid, enter a valid date"
}
//void Day::void firstAvailableHour(int t) // booking the first available hour in the day
//{
// clrscr();
for (int i=0;i<9;i++)
{ if (schedule[i] == true)
{ strcpy(schedule[i], "Booked");
cout << " sorry already booked " <<(i+9) <<" :00\n";
i=10; //supposed toend the loop
Sleep(2000);
}
else // if there is no booking
{
if (1==8)// no more time to book
{ cout<<"sorry no more time to book";
Sleep(2000); // go to bed for 2 secs
}
}
}
else //if no date is choosen
{
cout << "please enter date b4 boking";
Sleep(2000);
}
}
void Day::display()
{
std::cout<<day<<'/'<<month<<'/'<<year<<std::endl;//Display day
for(int i=0;i<9;++i)
std::cout<<(i<4?i+9:i-3)<<(i<3?"am":"pm")<<'\t'<<(schedule[i]?"Booked":"Available")
<<std::endl;
}
int main(void)
{
int d,m,y,t;
std::cout<<" =============Menu===========> \n";
std::cout<<" 1. Input time Appointment time between 9-5\n";
std::cout<<" 2. Enter 8 to display the avalaible time \n \n";
std::cout<<" Input the date:\n";
std::cin>>d>>m>>y;
Day MyDay(d,m,y);//Create a day
}
do{
std::cout<<"Input appointment time:\n";
std::cin>>t;
if(!MyDay.checkTime(t)) {
MyDay.display();
}
if(!MyDay.IfAnyTimeAvailable()){
break ;
}
}
while(1);
cout<<"********All Bookings************"<<endl ;
MyDay.display();
while(0);
cout<<"********All Bookings************"<<endl ;
MyDay.display();
return 0;
}