Doctor Appointment Booking Project using C++ Language

Last updated Jan 20, 2022

In this project, we will see how to build the DOCTOR APPOINTMENT BOOKING project using C++ programming language. This Doctor Appointment Booking project is a straightforward console program written in C++ with no visuals. This project will teach you how to utilize the stream class and how to handle files in the C++ programming language.
 

In this project, these following are the main features of project we going to learn:

  •  Manage Doctor Appointment Slots: It is used to create available slots which will be displayed when a patient/user wants to see or book the slots.
  •  Book Appointment : A user/patient can book appointment slot.
  •  See All Existing Slots : Using this we can check how many slot is available or booked.
     

Important: The appointment record will be saved even if the app is closed.
 

Tools Used using project:

I used Dev C++  IDE or you can use any IDE which is suitable to you.

 

Implementation of Project:

Step 1. Create a new Project in Dev C++.

File > New >  Source File  or Ctrl + N

 

Step 2. First of all we will include all Header file which will use in project.

#include
#include
#include
#include

 

Step 3.  Add the following line below header file:

using namespace std;

The using namespace statement simply implies that in the scope in which it is present, all items in the std namespace are available without needing to prefix them with std::
 

Step 4. Now create the I/o Stream and Associate with actual files, You can read about File and Streams in c++

It used to read any input or write any output, open the file streams and associate them with the actual files:

ofstream fout;
ifstream fin;

 

Step 5. Now we will create a function for bookAppointment and the code be like:

In this we use cout for print output data and int BookAppointment() is a function which returns a value because it used int before method. IF we used void BookAppointment that means it will not return any value.

int bookAppointment()
{
    system("cls");
    
    cout<<"\n ----- Book your appointment for particular time slot ---- \n";    
    cout<<"\n ----- Available slots are---- \n";     

    //check if record already exist..
    ifstream read;
    read.open("appointment.dat");
    
    int hoursbook = 8;
    
    int arr[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
    int recordFound =0; 
     
    if(read)
    {     
    string line;
    char key = 'A';
    int i = 9;
           
    while(getline(read, line)) {
    char temp = line[0];
    int index = (temp - 65);
    arr[index]=1;  
    recordFound = 1;
    }
    if(recordFound == 1)
    {
    cout<<"\n Appointment List by Hours:";
    char key = 'A';
    int hours = 9;
    for(int i = 0; i<=12; i++)
    {
    if(i == 0){
    if(arr[i] == 0) 
    cout<<"\n "< 0"<     else
    cout<<"\n "< 0"<     }
    
    else
    {                                               
    if(arr[i] == 0) 
    cout<<"\n "<"<     else
    cout<<"\n "<"<     }
    hours++; key++;
    }
            
    }
        
    read.close();
    }
    if(recordFound == 0){
    cout<<"\n Appointment Available for following hours :";
    char key = 'A';
    for(int i = 9; i<=21; i++)
    {
        if(i==9)
        cout<<"\n "< 0"<         else
        cout<<"\n "< "<         key++;
    }
        
    }
   
   char choice;
   cout<<"\n\n Choose your slot: ";
   cin>>choice;
   
   if( !(choice >= 'A' && choice <='Z'))
   {
    cout<"\n Error : Invalid Selection";
    cout<<"\n Please selction correct value from menu A- Z";
    cout<"\n Press any key to continue";
    getchar();getchar();
    system("cls");
    bookAppointment();
   }
   
   int index = (choice-65 );
   int isBooked = 1;
   if(arr[index] == 0) 
      isBooked = 0;
      
   if(isBooked ==1)
   {
       cout<<"\n Error : This Appointment  slot is already booked";
       cout<<"\n Please select different slot time !!";
       cout<<"\n Press any key to continue!!";
       getchar();getchar();
       system("cls");
       bookAppointment();
   }
   
   string name;
   cout<<"\n Enter your Name:";
   cin>>name;  
   
   ofstream out;
   out.open("appointment.dat", ios::app);
    
   if(out){
       out<        out.close();
       cout<<"\n Appointment is Successfully Booked for Hours : "<< (choice-65) + 9 <<" !!";
    }
    else
    {
        cout<<"\n Error while saving booking";
    }

    cout<<"\n Please any key to continue..";
    getchar(); getchar();
    return 0;    
    
}

 

Step 6. Next, we will create a new function existingAppointment() 

The existingAppointment() function is created because to get all the available slots of doctor. This function have variables, commands for print output, get input from users, file read, file open and show data, arrays for iterate data.

int existingAppointment()
{
    system("cls");
    
   cout<<"\n ----- List of Available Appointments ---- \n";
    //check if record already exist..
    ifstream read;
    read.open("appointment.dat");
    
    int hoursbook = 8;
    
    int arr[13] = {0,0,0,0,0,0,0,0,0,0,0,0,0};
    int recordFound =0; 
     
    if(read)
    {     
    string line;
    char key = 'A';
    int i = 9;
           
    while(getline(read, line)) {
    char temp = line[0];
    int index = (temp - 65);
    arr[index]=1;  
    recordFound = 1;
    }
    if(recordFound == 1)
    {
    cout<<"\n Appointment List by hours:";
    char key = 'A';
    int hours = 9;
    for(int i = 0; i<=12; i++)
    {
    if(i == 0){
    if(arr[i] == 0) 
    cout<<"\n "< 0"<     else
    cout<<"\n "< 0"<     }
    
    else
    {                                               
    if(arr[i] == 0) 
    cout<<"\n "<"<     else
    cout<<"\n "<"<     }
    hours++; key++;
    }
            
    }
        
    read.close();
    }
    if(recordFound == 0){
    cout<<"\n Appointment Available for following hours :";
    char key = 'A';
    for(int i = 9; i<=21; i++)
    {
        if(i==9)
        cout<<"\n "< 0"<         else
        cout<<"\n "< "<         key++;
    }
        
    }
   

   ofstream out;
   out.open("appointment.dat", ios::app);
    

    cout<<"\n Please any key to continue..";
    getchar(); getchar();
    return 0;     
}

 

Step 7. Finally, we will create a main method to call all functions and run the project

In main method we will make it int type because we want that main() method return data.

int main(int argc, char** argv) {
    while(1)
    {
        system("cls");
        cout<<"\t\tDoctor Appointment Booking System\n";
        cout<<"----------------------------------------\n\n";
        
        cout<<"  1. Book an Appointment\n";
        cout<<"  2. Check All Existing Appointments\n";
        cout<<"  0. Exit Now \n";
        int choice;
        
        cout<<"\n Enter you choice: ";
        cin>>choice;
        
        switch(choice)
        {
            case 1: bookAppointment(); break;
            case 2: existingAppointment(); break;
            case 0: 
                while(1)
                {
                 system("cls");
                    cout<<"\n Are you sure, you want to exit? y | n \n";
                    char ex;
                    cin>>ex;
                    if(ex == 'y' || ex == 'Y')
                        exit(0);
                    else if(ex == 'n' || ex == 'N')
                             {
                              break;
                            }
                     else{
                         cout<<"\n Invalid choice !!!";
                         getchar();
                     }
                  }    break;
                      
            default: cout<<"\n Invalid choice. Enter again ";
                     getchar();
                     
        }                   

    }
    return 0;
}   

 

As you see in code we use switch case in main() method 

On Case 1: We call bookAppointment() function which is used for booking the appointment.
On Case 2: We call existingAppointment() function which will display all existing appointments.
On Case 0: We use 0 to go back and finally close the application.


Step 8. Run the project and you will get the following output:
 

Main Screen:
The first screen when you run the project 

Cpp Doctor booking Appointment Project source code

 

Book your appointment Screen:
Slots will be displayed to you basis of hours with reference E.g: A, B, C and so on.. with a status Available or Booked.

Cpp Doctor booking Appointment Project source code 2

 

Appointment Booked Screen:
In this screen you will get a message of successfully booked.

Cpp Doctor booking Appointment Project source code 3

 

Already Booked Slot:
In case you select the booked appointment it will display you a following screen.

Cpp Doctor booking Appointment Project source code 4

 

Existing Appointment Screen:

Cpp Doctor booking Appointment Project source code 5

 

Exit Screen:

Cpp Doctor booking Appointment Project source code 6

 

Conclusion: In this project we have covered how to build project on Doctor Appointment Booking using C++ and how to book slots, get available slots data and so on.

 

Download Source code

 

Article Contributed By :
https://www.rrtutors.com/site_assets/profile/assets/img/avataaars.svg

4126 Views