Hospital Management System with C++

by | Jul 26, 2023 | Basic Coding, C/C++, Coding

Home » Coding » Hospital Management System with C++

This Hospital Management System with C++ simulates the management of patients across different departments in a hospital setting. This system can be used as a starting point for anyone aiming to develop a more comprehensive hospital management system or looking for a hands-on approach to understanding data structures and algorithms, particularly the use of linked lists.

Introduction

The main objective of this C++ project is to automate the management of a hospital’s departments and the patients within them, thereby simplifying and speeding up hospital processes. Our system manages four departments, namely the General Clinic, Heart Clinic, Lung Clinic, and Plastic Surgery. For each department, it maintains a queue of patients awaiting treatment, with provisions for adding patients, taking them out for treatment, searching for a specific patient, and displaying the list of patients in a department.

This program also follows a priority scheme based on the criticality of the patient’s condition. Critically ill patients are added at the beginning of the queue, ensuring they receive treatment on a priority basis. In contrast, regular patients are added at the end of the queue. This queue management follows a first-in, first-out (FIFO) protocol, which means patients are taken out for treatment in the order they were added.

Objectives

1. Creating Departments: To establish four departments in the hospital—General Clinic, Heart Clinic, Lung Clinic, and Plastic Surgery. Each department maintains a queue (linked list) of patients waiting for treatment.

2. Patient Management: To add patients to any department. There are two categories of patients – normal and critically ill. A critically ill patient is always added to the beginning of the queue (prioritizing their treatment), while a normal patient is added to the end of the queue.

3. Searching Patients: To provide the ability to search for a patient within a department using their unique ID.

4. Service Management: When a patient is taken to the doctor, they are removed from the queue. The system follows a first-in, first-out (FIFO) approach, meaning that the patient who arrived first (is at the front of the queue) gets treated first.

5. Displaying Patient List: To display the list of all patients in a particular department, showing details such as ID, name, age, blood group, and gender.

6. Repetitive Actions: The system keeps running until explicitly stopped by the user, allowing for continuous addition, removal, search, and listing of patients in different departments.

Requirements

You should have the following pre requisites to build this Hospital Management System with C++

Knowledge Prerequisites:

  • Basic C++ Knowledge: You need to understand the basics of C++, including variables, data types, operators, loops, and conditionals. This program uses all of these concepts.
  • Advanced C++ Knowledge: The code also uses more advanced concepts like structures (struct), classes, pointers, and dynamic memory allocation (new and delete). It’s crucial to understand how these work in C++.
  • File Input/Output: The code assumes that data is being read from or written to files. Understanding how C++ handles file I/O is important.
  • String Manipulation: Familiarity with string manipulation in C++ is essential, particularly with functions like getline, strcmp, and strcpy.
  • Knowledge of Linked Lists: The program uses a singly linked list to manage patient data. Linked lists are a fundamental data structure in computer science, so knowing how they work is crucial.

Software Prerequisites:

  • C++ Compiler: You need a compiler to build and run C++ code. The GNU Compiler Collection (GCC) is a commonly-used free compiler that can handle C++ code.
  • Integrated Development Environment (IDE): While not strictly necessary, an IDE makes writing, building, and debugging C++ code easier. Some popular C++ IDEs include Code::Blocks, Eclipse CDT, and Visual Studio.

The specific IDE you choose doesn’t significantly impact the code itself as long as the IDE supports C++ development. However, it may affect how you build and run the code. For example, in Visual Studio, you would typically use the built-in “Build Solution” and “Run” options to build and execute your code, whereas, in Code::Blocks, you would use the “Build” and “Run” buttons.

Note: Your IDE should be configured to use a C++ compiler (like GCC) to build your code.

Source Code

#include<iostream>
#include<conio.h>
#include<string.h>
#include<stdlib.h>
using namespace std;
struct patient
{
long long ID;
string firstname;
string lastname;
int age;
char blood[5];
char gender;
patient*next;
};
class linkedqueue
{
patient *head,*last;
public:
linkedqueue() //constructor
{
head=NULL;
last=NULL;
}
patient input();
void insertatend();
void insertatbeg();
void getpatientout();
void listofpatients();
int search(int);
char departmentname[50];
};
int linkedqueue :: search(int item)
{
if(head==NULL)
return false;
else
{
int flag=0;
patient*p=new patient();
p=head;
while( p->next!=NULL && p->ID!=item )
{
p=p->next;
}
if(p->ID==item)
{
flag=1;
return true;
}
if(flag==0)
return false;
}
}
int readnumber()
{
char b[20];
cin.getline(b, sizeof(b));
return atoi(b);
}
patient linkedqueue :: input()
{
int flag=0;
patient *p=new patient();
cout << "\n Please enter data for patient\n";
cout<<"\n First name : ";
getline(cin,p->firstname);
cout << " Last name : ";
getline(cin,p->lastname);
again :
cout << " Blood Group : ";
cin>>p->blood;
if((strcmp(p->blood,"A+")==0)||(strcmp(p->blood,"a+")==0)||(strcmp(p->blood,"A-")==0)||(strcmp(p->blood,"a-")==0)||
(strcmp(p->blood,"B+")==0)||(strcmp(p->blood,"b+")==0)||(strcmp(p->blood,"B-")==0)||(strcmp(p->blood,"b-")==0)||
(strcmp(p->blood,"O+")==0)||(strcmp(p->blood,"o+")==0)||(strcmp(p->blood,"O-")==0)||(strcmp(p->blood,"o-")==0)||
(strcmp(p->blood,"AB+")==0)||(strcmp(p->blood,"ab+")==0)||(strcmp(p->blood,"AB-")==0)||(strcmp(p->blood,"ab-")==0))
flag=1;
if(flag==0)
{
cout<<"\n Invalid Blood Group Try Again..\n\n";
goto again;
}
cout<<" Gender(m/f) : ";
cin>>p->gender;
cout<<" Age : ";
cin>>p->age;
cout<<" Mobile number : ";
cin>>p->ID;
if(search(p->ID))
{
p->ID=0;
cout << "\n Data not valid. Operation cancelled.";
}
return *p;
}
void output(patient *p)
{
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n Patient data:\n";
cout<<"\n First Name : "<<p->firstname;
cout<<"\n Last Name : "<<p->lastname;
cout<<"\n Gender : "<<p->gender;
cout<<"\n Age : "<<p->age;
cout<<"\n Blood Group : "<<p->blood;
cout<<"\n Mobile Number : "<<p->ID;
cout<<"\n\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
}
void linkedqueue :: insertatbeg()
{
patient*p=new patient();
*p=input();
if(p->ID==0)
return;
if(head==NULL)
{
head=p;
last=p;
p->next=NULL;
}
else
{
p->next=head;
head=p;
}
system("cls");
cout << "\n\tPatient added:";
output(p);
}
void linkedqueue:: insertatend()
{
patient*p=new patient();
*p=input();
if(p->ID==0)
return;
if(head==NULL)
{
head=p;
last=p;
p->next=NULL;
}
else
{
p->next=NULL;
last->next=p;
last=p;
}
system("cls");
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n |-- HOSPITAL MANAGEMENT SYSTEM --|";
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n";
cout <<"\n ----------PATIENT ADDED-----------";
output(p);
}
void linkedqueue :: getpatientout()
{
system("cls");
if(head==NULL)
{
cout<<"\n No Patient to operate";
}
else
{
patient*p=new patient();
p=head;
head=head->next;
cout << "\n Patient to operate:";
output(p);
}
}
void linkedqueue :: listofpatients()
{
if(head==NULL)
{
cout<<"\n No patient";
}
system("cls");
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n |-- HOSPITAL MANAGEMENT SYSTEM --|";
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n";
patient*p=new patient;
p=head;
while(p!=NULL)
{
cout<<"\n Patient data:\n";
cout<<"\n First Name : "<<p->firstname;
cout<<"\n Last Name : "<<p->lastname;
cout<<"\n Gender : "<<p->gender;
cout<<"\n Age : "<<p->age;
cout<<"\n Blood Group : "<<p->blood;
cout<<"\n Mobile Number : "<<p->ID;
cout<<"\n\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n";
p=p->next;
}
cout<<"\n";
}
void departmentmenu (linkedqueue * q)
{
int choice = 0, success;
patient p;
while (choice != 5)
{
system("cls");
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n |-- HOSPITAL MANAGEMENT SYSTEM --|";
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n\n "<<q->departmentname;
cout<<"\n [1] Add normal patient\n";
cout<<" [2] Add critically ill patient\n";
cout<<" [3] Take patient to Doctor\n";
cout<<" [4] Display list\n";
cout<<" [5] Change department or exit\n";
cout<<"\n Please enter your choice : ";
choice=readnumber();
cout<<" \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
switch (choice)
{
case 1: q->insertatend();
cout << "\n Press any key";
getch();
break;
case 2: q->insertatbeg();
cout << "\n Press any key";
getch();
break;
case 3: q->getpatientout();
cout<<"\n Press any key";
getch();
break;
case 4: system("cls");
q->listofpatients();
cout<<"\n Press any key";
getch();
break;
}
}
}
int main ()
{
int i, choice = 0;
linkedqueue departments[4];
while(choice!=5)
{
strcpy(departments[0].departmentname,"GENERAL CLINIC\n");
strcpy(departments[1].departmentname,"HEART CLINIC\n");
strcpy(departments[2].departmentname,"LUNG CLINIC\n");
strcpy(departments[3].departmentname,"PLASTIC SURGERY\n");
system("cls");
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd";
cout<<"\n |-- HOSPITAL MANAGEMENT SYSTEM --|";
cout<<"\n \xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\xcd\n\n";
cout<<" Main Menu\n\n";
for (i = 0; i < 4; i++)
{
cout<<" "<<(i+1)<<": "<<departments[i].departmentname;
}
cout<<" 5: Exit";
cout<<"\n\n Please enter your choice : ";
choice=readnumber();
if(choice>=1 && choice<=4)
{
departmentmenu(&departments[choice-1]);
}
}
if(choice==5)
cout<<"\n\t\tThank you! \n";
exit(0);
}

Explanation of the Code

The provided code is a simulation of a hospital management system using a linked list data structure in C++. Here’s a summary of what the code is doing:

1. struct patient: The patient struct stores data for individual patients (ID, name, age, blood group, gender, and a pointer to the next patient).

2. class linkedqueue: The linkedqueue class contains two pointers (head and last) pointing to the first and last patient in the linked list. It also contains the member functions required to perform operations on this linked list:

  • search(): Searches for a patient with a given ID in the linked list.
  • input(): Allows user to input patient information and checks if the entered blood group and ID are valid. The ID is considered invalid if a patient with the same ID already exists.
  • insertatbeg() and insertatend(): Adds a new patient to the beginning or end of the linked list, respectively.
  • getpatientout(): Deletes the first patient from the linked list.
  • listofpatients(): Prints out the data of all patients in the linked list.

3. departmentmenu(): This function is called for each department and allows the user to perform the following operations:

  • Add a normal patient (insert at end)
  • Add a critically ill patient (insert at beginning)
  • Take patient to the doctor (remove the first patient in the list)
  • Display list of patients

4. main(): In the main() function, four departments (general clinic, heart clinic, lung clinic, and plastic surgery) are created as linked queues. Then, in a loop, the user can select a department and call the departmentmenu() function to perform operations on the selected department. The loop continues until the user chooses to exit.

Output

Conclusion

The idea behind the development of this Hospital management system with C++ is to maintain the records of patients according to their diagnosis information. Our system is interactive, with user-friendly prompts that guide the user through the various operations they can perform. This C++ project serves as a practical tool for learning C++ and exploring how linked lists can be used to manage and manipulate data efficiently in real-world applications.

We can also add more features to this project like Doctor’s records, their availability, appointments, and so on. Thus, developing this project will prove very beneficial for healthcare institutes.

More C++ Projects>>>

You May Also Like To Create…