Thursday, March 13, 2025

C++ program for a hospital

Here's a simple C++ program for a hospital:





#include <iostream>
#include <string>
using namespace std;

class Patient {
private:
    string name;
    int age;
    string disease;
    string doctor;

public:
    void inputDetails() {
        cout << "Enter patient name: ";
        cin.ignore();
        getline(cin, name);
        cout << "Enter patient age: ";
        cin >> age;
        cout << "Enter patient disease: ";
        cin.ignore();
        getline(cin, disease);
        cout << "Enter doctor's name: ";
        getline(cin, doctor);
    }

    void displayDetails() {
        cout << "\nPatient Details\n";
        cout << "Name: " << name << endl;
        cout << "Age: " << age << endl;
        cout << "Disease: " << disease << endl;
        cout << "Doctor: " << doctor << endl;
    }
};

class Hospital {
private:
    string hospitalName;
    string address;
    Patient patients[10];
    int numberOfPatients;

public:
    void inputHospitalDetails() {
        cout << "Enter hospital name: ";
        cin.ignore();
        getline(cin, hospitalName);
        cout << "Enter hospital address: ";
        getline(cin, address);
    }

    void addPatient() {
        patients[numberOfPatients].inputDetails();
        numberOfPatients++;
    }

    void displayPatientDetails() {
        for (int i = 0; i < numberOfPatients; i++) {
            patients[i].displayDetails();
        }
    }

    void displayHospitalDetails() {
        cout << "\nHospital Details\n";
        cout << "Name: " << hospitalName << endl;
        cout << "Address: " << address << endl;
    }
};

int main() {
    Hospital hospital;
    int choice;
    hospital.numberOfPatients = 0;
    hospital.inputHospitalDetails();
    while (true) {
        cout << "\nHospital Management System\n";
        cout << "1. Add patient\n";
        cout << "2. Display patient details\n";
        cout << "3. Display hospital details\n";
        cout << "4. Exit\n";
        cout << "Enter your choice: ";
        cin >> choice;
        switch (choice) {
            case 1:
                hospital.addPatient();
                break;
            case 2:
                hospital.displayPatientDetails();
                break;
            case 3:
                hospital.displayHospitalDetails();
                break;
            case 4:
                return 0;
            default:
                cout << "Invalid choice. Please try again.\n";
        }
    }
    return 0;
}


C++ program for a restaurant

 Here's a simple C++ program for a restaurant:




#include <iostream>

#include <string>

using namespace std;


class Restaurant {

private:

    string itemName[10];

    int itemPrice[10];

    int itemQuantity[10];

    int numberOfItems;


public:

    void addItem() {

        cout << "Enter item name: ";

        cin.ignore();

        getline(cin, itemName[numberOfItems]);

        cout << "Enter item price: ";

        cin >> itemPrice[numberOfItems];

        cout << "Enter item quantity: ";

        cin >> itemQuantity[numberOfItems];

        numberOfItems++;

    }


    void displayMenu() {

        cout << "\nRestaurant Menu\n";

        for (int i = 0; i < numberOfItems; i++) {

            cout << (i + 1) << ". " << itemName[i] << " - $" << itemPrice[i] << " (" << itemQuantity[i] << " available)\n";

        }

    }


    void placeOrder() {

        int choice, quantity;

        cout << "Enter the item number to order: ";

        cin >> choice;

        cout << "Enter the quantity to order: ";

        cin >> quantity;

        if (choice > 0 && choice <= numberOfItems && quantity <= itemQuantity[choice - 1]) {

            cout << "Order placed successfully!\n";

            itemQuantity[choice - 1] -= quantity;

        } else {

            cout << "Invalid choice or insufficient quantity.\n";

        }

    }


    void displayOrderSummary() {

        cout << "\nOrder Summary\n";

        for (int i = 0; i < numberOfItems; i++) {

            if (itemQuantity[i] < itemQuantity[i]) {

                cout << itemName[i] << " x " << (itemQuantity[i] - itemQuantity[i]) << "\n";

            }

        }

    }

};


int main() {

    Restaurant restaurant;

    int choice;

    restaurant.numberOfItems = 0;

    while (true) {

        cout << "\nRestaurant Management System\n";

        cout << "1. Add item to menu\n";

        cout << "2. Display menu\n";

        cout << "3. Place order\n";

        cout << "4. Display order summary\n";

        cout << "5. Exit\n";

        cout << "Enter your choice: ";

        cin >> choice;

        switch (choice) {

            case 1:

                restaurant.addItem();

                break;

            case 2:

                restaurant.displayMenu();

                break;

            case 3:

                restaurant.placeOrder();

                break;

            case 4:

                restaurant.displayOrderSummary();

                break;

            case 5:

                return 0;

            default:

                cout << "Invalid choice. Please try again.\n";

        }

    }

    return 0;

}



*Example Output:*


Restaurant Management System

1. Add item to menu

2. Display menu

3. Place order

4. Display order summary

5. Exit

Enter your choice: 1


Enter item name: Burger

Enter item price: 10

Enter item quantity: 5


Restaurant Management System

1. Add item to menu

2. Display menu

3. Place order

4. Display order summary

5. Exit

Enter your choice: 2


Restaurant Menu

1. Burger - $10 (5 available)


Restaurant Management System

1. Add item to menu

2. Display menu

3. Place order

4. Display order summary

5. Exit

Enter your choice: 3


Enter the item number to order: 1

Enter the quantity to order: 2


Order placed successfully!


Restaurant Management System

1. Add item to menu

2. Display menu

3. Place order

4. Display order summary

5. Exit

Enter your choice: 4


Order Summary

Burger x 2



This program defines a `Restaurant` class with private member variables for menu items, prices, and quantities. The class includes public methods for adding items to the menu, displaying the menu, placing orders, and displaying order summaries.


In the `main()` function, a `Restaurant` object is created, and a simple text-based menu is displayed. The user can interact with the program by choosing options to add items, display the menu, place orders, or display order summaries.

Featured posts

स्मार्ट इन्वेस्टिंग फॉर बिगिनर्स:

 स्मार्ट इन्वेस्टिंग फॉर बिगिनर्स:  पैसिव इनकम कमाने का आसान रास्ता"।  इसमें मैंने बताया है कि कैसे छोटी रकम से शुरुआत करके, म्यूचुअल फ...

Popular posts