: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;
}

Recommended Answers

All 6 Replies

if (d <= 31 && d >= 1 && m <= 12 && m >= 1 && y <=2006)

This was the first problem I noticed. Put your individual comparisons inside brackets. Think of each comparison as an individual expression.

if ( (d<=31) && (d>=1) &&
     (m<=12) && (m>=1) &&
     (y<=2006) )

Personally, I would break this down into 3 seperate checks - this allows you to give more specific information about which part of the date went wrong, eg,

if ( (d<1) && (d>31) )
    cout << "ERROR: Out of range for day" << endl;

anything on the 2nd one (no 2)

> { if (schedule == true)
> { strcpy(schedule, "Booked");
The strcpy() makes no sense - schedule is an array of bool, not a string.
Does your compiler even warn you about this?
Or do you just ignore all warnings and just try it anyway?

i have been looking at this code for days, even a single spelling mistake i cannot pick out, combine with my shady knowledge of c++. i will be glad if you can send me a compiled code.
thanks men

its strange, amazing, how can u do this many syntactical erros?? well i m trying my best to correct your "checkDate" function....

please go through this code carefully and try to findout if it can help u...
i think u should try few books and u should give your honest efforts to write a program on paper first..
best of luck..

#include<iostream>
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 static checkDate(int,int,int);
       //==========================
       int firstAvailableHour()// Get the first avalaiable hour in the appointment l
	   {
		   if(IfAnyTimeAvailable) {
			   for(int i=0;i<9;++i) {
				   if(!schedule[i]) {
					   return i ;//time is available for booking
					}
				}
		   }
		   else {
			   cout<<"No time is available"<<endl;
			   return 0;
		   }
	   }
       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)  //; who asked u to put this here??
{	
	if (d <= 31 && d >= 1 && m <= 12 && m >= 1 && y <=2006) 
	{
	  cout<<" Date is kindly Accepted"<<endl;
	}
	else
	{
	  cout<<"\ndate is invalid, enter a valid date"<<endl; ;
	  cin>>d >> m >> y ;
	  checkDate(d,m,y) ;
	  return ;
	}
}


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::checkDate(d,m,y);
   Day MyDay(d,m,y);//Create a day 

   do{
	   std::cout<<"Input appointment time:\n";
	   std::cout<<"or input 0 to get first available time\n";
       std::cin>>t;
	   if(t==0){
		   t = MyDay.firstAvailableHour() ;
		   cout<<" Wrking hour ::"<<t+1<<" is available"<<endl;
		   std::cout<<"Input appointment time:\n";
		   std::cin>>t;
	   }
	   if(!MyDay.checkTime(t)) {
		   MyDay.display();
	   }
	   if(!MyDay.IfAnyTimeAvailable()){
		   break ;
	   }
	   cout<<"Booking Confirmed "<<endl ;
	   int check;
	   cout<<"Input 1: To Book Furthur\n"
		   <<"Input Anything: To End"<<endl ;
	   cout<<"	:: ";
	   cin>>check ;
	   if(1==check){
		   continue ;
	   }
	   else {
		   break ;
	   }
	 }
	while(1);
	cout<<"********All Bookings************"<<endl ;
	MyDay.display();

return 0;
}
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.