0

Hey ! i am trying to create a program of hospital management system using doubly linked list in c++.

in this i am using whole class for patient data. i created a nodes for linked list . i am using one node to represent one bed in hospital. but after that when i am trying to print some data manually , nothing is printing in console and program is ending without showing any output

this is my code :

#define max_limit 25;// maximum 50 beds can be there in hospital
using namespace std;

class patient  //creating class to store the data of patient
{
 public:
 int pt_id;
 string pt_name; //patient name
 string diagnosis; //diagnosis patient have

};
struct bed  //each bed in hospital
{

 class  patient p;
 struct bed *prev;
 struct bed *next;

};


int main()
{
    //creating 3 beds in starting only and giving values.
    struct bed *head=(struct bed *)malloc(sizeof(patient));
    struct bed *second=(struct bed *)malloc(sizeof(patient));
    struct bed *tail=(struct bed *)malloc(sizeof(patient));
    

    head->prev=NULL;
    head->p.pt_id=6478;
    head->p.pt_name="Jayraj";
    head->p.diagnosis="Headaches";
    head->next=second;

    second->prev=head;
    second->p.pt_id=8933;
    second->p.pt_name="Mayank";
    second->p.diagnosis="Highfever";
    second->next=tail;
    
    tail->prev=second;
    tail->p.pt_id=1788;
    tail->p.pt_name="Jay";
    tail->p.diagnosis="Stomacheache";
    tail->next=NULL;

cout<<"Size:"<<sizeof(patient);



    return 0;
}```



ANXIETY
  • 21
  • 4
  • Whenever you need to use C-style casting in C++, like you do with the result of the `malloc` calls, you should take that as a sign that you're doing something wrong. And in this case the "wrong" you're doing is to use `malloc` to allocate the structures instead of the `new` operator. It almost seems like you're using a C teaching resource to learn C++, and that will not work because C and C++ are two *very* different languages. Please invest in [some good **C++** books](https://stackoverflow.com/questions/388242/the-definitive-c-book-guide-and-list/388282#388282). – Some programmer dude Feb 17 '22 at 05:58
  • And looking at the macro definition for `max_limit` it seems your C resource doesn't do a good job either, as that macro definition will most likely not work. – Some programmer dude Feb 17 '22 at 06:01
  • Lastly about what happens: You have a *crash*. The crash happens because you don't properly construct your objects. A couple of seconds in a ***debugger*** should have at least told you that you had a crash. – Some programmer dude Feb 17 '22 at 06:02

1 Answers1

0

You should check the errors - make sure your environment displays them. There are at least these issues:

  • When allocating a bed you shouldn't use sizeof(patient), but sizeof(bed).
  • define should not end with a semi-colon.
  • As a bed could be empty, you'd better define the p member as a pointer to a patient instance
  • You're not using C++ syntax:
    • Use new instead of malloc.
    • Define constructors and methods
    • Use nullptr instead of NULL
    • Use PascalCase for class names

Here is some code for inspiration, but you'll need to add more logic to it for your assignment (e.g. you'll have to verify the limit of beds):

#define max_limit 25 // maximum 25 beds allowed in hospital
using namespace std;

class Patient
{
public:
    int pt_id;
    string pt_name; //patient name
    string diagnosis; //diagnosis patient have

    // Constructor:
    Patient(int id, string name, string diag) : 
        pt_id(id), pt_name(name), diagnosis(diag) {}
};

class Bed
{
public:
    Patient* p = nullptr;
    Bed* prev = nullptr;
    Bed* next = nullptr;

    // Constructors:
    Bed() {};
    Bed(Patient* p) : p(p) {};

    // Method to ease linking beds
    Bed* prepend(Bed* other) {
        other->next = this;
        this->prev = other;
        return other;
    };
};

int main()
{
    // Use constructor to create patients
    Patient *jaray = new Patient(6478, "Jayraj", "Headaches");
    Patient *mayank = new Patient(8933, "Mayank", "Highfever");
    Patient *jay = new Patient(1788, "Jay", "Stomacheache");

    // Use constructor and method to create linked list
    Bed *beds = (new Bed(jay))
         ->prepend(new Bed(mayank))
         ->prepend(new Bed(jaray));

    // Let's output something: the beds with patient names
    Bed *bed = beds;
    while (bed != nullptr) {
        if (bed->p == nullptr) {
            cout << "bed without patient\n";
        } else {
            cout << "bed occupied by " << bed->p->pt_name << "\n";
        }
        bed = bed->next;
    } 

    return 0;
}
trincot
  • 317,000
  • 35
  • 244
  • 286