{}

Build your resume with HTML & CSS and win $100

Get featured on Programiz PRO and the Wall of Inspiration.

Build your resume with HTML & CSS and win $100

Join Challenge →
Join Challenge →

Build your resume with HTML & CSS and win $100

Get featured on Programiz PRO and the Wall of Inspiration.

Build your resume with HTML & CSS and win $100

Join Challenge →
Join Challenge →
run-icon
main.cpp
#include <iostream> #include <vector> #include <string> #include <cctype> using namespace std; // Class to store patient data class Patient { private: int id; string name; int age; string gender; string disease; string phone; string bloodGroup; string address; public: Patient(int pid, string pname, int page, string pgender, string pdisease, string pphone, string pblood, string paddress) { id = pid; name = pname; age = page; gender = pgender; disease = pdisease; phone = pphone; bloodGroup = pblood; address = paddress; } int getId() { return id; } void update(string pname, int page, string pgender, string pdisease, string pphone, string pblood, string paddress) { name = pname; age = page; gender = pgender; disease = pdisease; phone = pphone; bloodGroup = pblood; address = paddress; } void display() { cout << "\nPatient ID: " << id; cout << "\nName: " << name; cout << "\nAge: " << age; cout << "\nGender: " << gender; cout << "\nDisease: " << disease; cout << "\nPhone: " << phone; cout << "\nBlood Group: " << bloodGroup; cout << "\nAddress: " << address << endl; } }; // Hospital management class class Hospital { private: vector<Patient> patients; // 🔑 Helper function: Phone number validation string getValidPhone() { string phone; while (true) { cout << "Enter Phone Number : "; getline(cin, phone); bool isValid = (phone.size() == 10); for (char c : phone) { if (!isdigit(c)) { // check if all characters are digits isValid = false; break; } } if (isValid) break; cout << "❌ Invalid phone number! Please enter exactly 10 digits.\n"; } return phone; } public: void addPatient() { int id, age; string name, gender, disease, phone, bloodGroup, address; cout << "\nEnter Patient ID: "; cin >> id; cin.ignore(); cout << "Enter Name: "; getline(cin, name); cout << "Enter Age: "; cin >> age; cin.ignore(); cout << "Enter Gender (Male/Female): "; getline(cin, gender); cout << "Enter Disease: "; getline(cin, disease); // ✅ 10-digit phone check phone = getValidPhone(); cout << "Enter Blood Group: "; getline(cin, bloodGroup); cout << "Enter Address: "; getline(cin, address); Patient newPatient(id, name, age, gender, disease, phone, bloodGroup, address); patients.push_back(newPatient); cout << "\n✅ Patient record added successfully!" << endl; } void displayPatients() { if (patients.empty()) { cout << "\nNo patients in the record!" << endl; return; } cout << "\n--- Patient Records ---" << endl; for (auto &p : patients) { p.display(); } } void searchPatient() { int id; cout << "\nEnter Patient ID to search: "; cin >> id; for (auto &p : patients) { if (p.getId() == id) { cout << "\nPatient found!" << endl; p.display(); return; } } cout << "\nPatient not found!" << endl; } void updatePatient() { int id, age; string name, gender, disease, phone, bloodGroup, address; cout << "\nEnter Patient ID to update: "; cin >> id; cin.ignore(); for (auto &p : patients) { if (p.getId() == id) { cout << "Enter New Name: "; getline(cin, name); cout << "Enter New Age: "; cin >> age; cin.ignore(); cout << "Enter New Gender: "; getline(cin, gender); cout << "Enter New Disease: "; getline(cin, disease); // ✅ 10-digit phone check phone = getValidPhone(); cout << "Enter New Blood Group: "; getline(cin, bloodGroup); cout << "Enter New Address: "; getline(cin, address); p.update(name, age, gender, disease, phone, bloodGroup, address); cout << "\n✅ Patient record updated successfully!" << endl; return; } } cout << "\nPatient not found!" << endl; } void deletePatient() { int id; cout << "\nEnter Patient ID to delete: "; cin >> id; for (auto it = patients.begin(); it != patients.end(); ++it) { if (it->getId() == id) { patients.erase(it); cout << "\n✅ Patient record deleted successfully!" << endl; return; } } cout << "\nPatient not found!" << endl; } }; // Main program int main() { Hospital hospital; int choice; do { cout << "\n--- Hospital Management System ---"; cout << "\n1. Add Patient"; cout << "\n2. Display All Patients"; cout << "\n3. Search Patient by ID"; cout << "\n4. Update Patient"; cout << "\n5. Delete Patient"; cout << "\n6. Exit"; cout << "\nEnter your choice: "; cin >> choice; cin.ignore(); switch (choice) { case 1: hospital.addPatient(); break; case 2: hospital.displayPatients(); break; case 3: hospital.searchPatient(); break; case 4: hospital.updatePatient(); break; case 5: hospital.deletePatient(); break; case 6: cout << "\nExiting system. Goodbye!" << endl; break; default: cout << "\nInvalid choice! Try again." << endl; } } while (choice != 6); return 0; }
Output