Hospital Management System - part 2 Arrays
Code
#include <stdio.h>
#include <string.h>
#define MAX_PATIENTS 100
#define MAX_APPOINTMENTS 100
// Arrays to hold patient data
char patientNames[MAX_PATIENTS][100];
int patientAges[MAX_PATIENTS];
char patientDiseases[MAX_PATIENTS][100];
// Arrays to hold appointment data
char appointmentDates[MAX_APPOINTMENTS][20];
char appointmentTimes[MAX_APPOINTMENTS][10];
char appointmentPatientNames[MAX_APPOINTMENTS][100];
int main() {
int patientCount = 0;
int appointmentCount = 0;
int choice;
while (1) {
printf("\nHospital Management System\n");
printf("1. Add Patient\n");
printf("2. View All Patients\n");
printf("3. Add Appointment\n");
printf("4. View All Appointments\n");
printf("5. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);
switch (choice) {
case 1:
// Add Patient
if (patientCount >= MAX_PATIENTS) {
printf("Cannot add more patients, database is full!\n");
} else {
printf("\nEnter Patient Name: ");
getchar(); // to clear the newline character left by previous scanf
fgets(patientNames[patientCount], 100, stdin);
patientNames[patientCount][strcspn(patientNames[patientCount], "\n")] = '\0'; //
Remove trailing newline
printf("Enter Patient Age: ");
scanf("%d", &patientAges[patientCount]);
printf("Enter Patient Disease: ");
getchar(); // to clear the newline character left by previous scanf
fgets(patientDiseases[patientCount], 100, stdin);
patientDiseases[patientCount][strcspn(patientDiseases[patientCount], "\n")] = '\0'; //
Remove trailing newline
patientCount++;
printf("Patient added successfully!\n");
break;
case 2:
// View All Patients
if (patientCount == 0) {
printf("No patients in the database.\n");
} else {
printf("\nList of Patients:\n");
for (int i = 0; i < patientCount; i++) {
printf("Patient %d:\n", i + 1);
printf("Name: %s\n", patientNames[i]);
printf("Age: %d\n", patientAges[i]);
printf("Disease: %s\n", patientDiseases[i]);
printf("----------------------------\n");
break;
case 3:
// Add Appointment
if (appointmentCount >= MAX_APPOINTMENTS) {
printf("Cannot add more appointments, database is full!\n");
} else if (patientCount == 0) {
printf("No patients available to schedule appointments!\n");
} else {
printf("\nEnter Appointment Date (YYYY-MM-DD): ");
getchar(); // to clear the newline character left by previous scanf
fgets(appointmentDates[appointmentCount], 20, stdin);
appointmentDates[appointmentCount][strcspn(appointmentDates[appointmentCount],
"\n")] = '\0'; // Remove trailing newline
printf("Enter Appointment Time (HH:MM): ");
fgets(appointmentTimes[appointmentCount], 10, stdin);
appointmentTimes[appointmentCount][strcspn(appointmentTimes[appointmentCount],
"\n")] = '\0'; // Remove trailing newline
printf("Enter Patient Name for Appointment: ");
fgets(appointmentPatientNames[appointmentCount], 100, stdin);
appointmentPatientNames[appointmentCount]
[strcspn(appointmentPatientNames[appointmentCount], "\n")] = '\0'; // Remove trailing
newline
appointmentCount++;
printf("Appointment added successfully!\n");
break;
case 4:
// View All Appointments
if (appointmentCount == 0) {
printf("No appointments scheduled.\n");
} else {
printf("\nList of Appointments:\n");
for (int i = 0; i < appointmentCount; i++) {
printf("Appointment %d:\n", i + 1);
printf("Date: %s\n", appointmentDates[i]);
printf("Time: %s\n", appointmentTimes[i]);
printf("Patient Name: %s\n", appointmentPatientNames[i]);
printf("----------------------------\n");
break;
case 5:
printf("Exiting the system...\n");
return 0;
default:
printf("Invalid choice! Please try again.\n");
return 0;
}
OUTPUT
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 1
Enter Patient Name: arun
Enter Patient Age: 24
Enter Patient Disease: coma
Patient added successfully!
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 1
Enter Patient Name: aadi
Enter Patient Age: 24
Enter Patient Disease: ulcer
Patient added successfully!
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 2
List of Patients:
Patient 1:
Name: arun
Age: 24
Disease: coma
----------------------------
Patient 2:
Name: aadi
Age: 24
Disease: ulcer
----------------------------
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 3
Enter Appointment Date (YYYY-MM-DD): 2024-11-24
Enter Appointment Time (HH:MM): 21:23
Enter Patient Name for Appointment: arun
Appointment added successfully!
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 4
List of Appointments:
Appointment 1:
Date: 2024-11-24
Time: 21:23
Patient Name: arun
----------------------------
Hospital Management System
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Enter your choice: 5
Exiting the system...
Algorithm:
Step 1: Initialization
Initialize:
o patientCount = 0 (To keep track of the number of patients)
o appointmentCount = 0 (To keep track of the number of appointments)
o choice (To store user’s menu choice)
Step 2: Display Menu
Display options:
1. Add Patient
2. View All Patients
3. Add Appointment
4. View All Appointments
5. Exit
Step 3: User Input
Prompt the user to enter their choice (choice).
Step 4: Process User Choice
If choice == 1 (Add Patient):
o If patientCount < MAX_PATIENTS:
Get patient name, age, and disease.
Store data in respective arrays.
Increment patientCount.
Display "Patient added successfully!"
o Else: Display "Patient database full!"
If choice == 2 (View All Patients):
o If patientCount > 0:
Loop through the patient data and display all patients.
o Else: Display "No patients in the database."
If choice == 3 (Add Appointment):
o If appointmentCount < MAX_APPOINTMENTS:
If patientCount > 0:
Get appointment date, time, and patient name.
Store data in respective arrays.
Increment appointmentCount.
Display "Appointment added successfully!"
Else: Display "No patients to schedule appointments."
o Else: Display "Appointment database full!"
If choice == 4 (View All Appointments):
o If appointmentCount > 0:
Loop through the appointment data and display all appointments.
o Else: Display "No appointments scheduled."
If choice == 5 (Exit):
o Display "Exiting the system."
o End the program.
If choice is invalid:
o Display "Invalid choice, try again."
Step 5: Repeat
Go back to Step 2 to display the menu again, unless the user chooses to exit.
Pseudocode:
Initialize patientCount = 0, appointmentCount = 0
Initialize arrays: patientNames[], patientAges[], patientDiseases[], appointmentDates[],
appointmentTimes[], appointmentPatientNames[]
Repeat:
Display the menu with options (Add Patient, View All Patients, Add Appointment, View All
Appointments, Exit)
Prompt user for input (choice)
If choice == 1:
If patientCount < MAX_PATIENTS:
Get patient name, age, and disease
Store data in patientNames[], patientAges[], patientDiseases[]
Increment patientCount
Display "Patient added successfully!"
Else:
Display "Cannot add more patients, database is full!"
Else if choice == 2:
If patientCount > 0:
For i from 0 to patientCount-1:
Display patient data (name, age, disease)
Else:
Display "No patients in the database."
Else if choice == 3:
If appointmentCount < MAX_APPOINTMENTS:
If patientCount > 0:
Get appointment date, time, and patient name
Store data in appointmentDates[], appointmentTimes[], appointmentPatientNames[]
Increment appointmentCount
Display "Appointment added successfully!"
Else:
Display "No patients available to schedule appointments!"
Else:
Display "Cannot add more appointments, database is full!"
Else if choice == 4:
If appointmentCount > 0:
For i from 0 to appointmentCount-1:
Display appointment data (date, time, patient name)
Else:
Display "No appointments scheduled."
Else if choice == 5:
Display "Exiting the system..."
Exit program
Else:
Display "Invalid choice! Please try again."
End Repeat