0

lot of questions like this are there but i tried most of them but i couldn't success. as my last try i came up without any errors but the function does not work

this is what i did last i need to remove the specific object from list

void discharge_patients() {
    string str;
    cout << "Enter Patients Name : " << endl;
    cin >> str;

    for (hospital const& common : hospitalList) {
        if(common.getPatientName() == str){
            cout << "\nPatient Name : " << common.getPatientName() << "\t";
              hospitalList.remove(common);
              break;
        }
    }
    iputData();
}

declaration of hospitalList

list<hospital> hospitalList;

this is how i add objects to hospitalList

void admit_patient() {
    hospital obj;
    string str;
    int val;
    cout << "Enter Patients Name : " << endl;
    cin >> str;
    obj.setPatientName(str);
    cout << "Enter Bed Number : " << endl;
    cin >> val;
    obj.setBedNum(val);
    cout << "Enter Date of Admit 'dd/mm/yyyy' : " << endl;
    cin >> str;
    obj.setDateOfAdmit(str);
    cout << "Enter Patient Age :" << endl;
    cin >> val;
    obj.setAge(val);
    cout << "Enter Patient Ward No :" << endl;
    cin >> val;
    obj.setWardNumber(val);
    //push the object to hospitalList
    hospitalList.push_back(obj);
    iputData();
}

this is how my hospital class looks like

class hospital {

public: string Patient_Name, Date_of_Admit; int Bed_Num, Age, Ward_Number;

int getAge() const {
    return Age;
}

void setAge(int age) {
    Age = age;
}

int getBedNum() const {
    return Bed_Num;
}

void setBedNum(int bedNum) {
    Bed_Num = bedNum;
}

const string& getPatientName() const {
    return Patient_Name;
}

void setPatientName(const string& patientName) {
    Patient_Name = patientName;
}

int getWardNumber() const {
    return Ward_Number;
}

void setWardNumber(int wardNumber) {
    Ward_Number = wardNumber;
}

const string& getDateOfAdmit() const {
    return Date_of_Admit;
}

void setDateOfAdmit(const string& dateOfAdmit) {
    Date_of_Admit = dateOfAdmit;
}
};

1 Answer 1

2

Your iterator becomes invalid if you change the list during iteration.

See https://stackoverflow.com/a/10360466/5107799 or https://stackoverflow.com/a/1016332/5107799

Sign up to request clarification or add additional context in comments.

4 Comments

To add to that point, instead of using a for-loop, consider calling std::find() from <algorithm>. This way you avoid manipulation while iterating.
Or simply remove_if from the second link
Yes, but OP's goal is still to remove. So by getting the iterator from std::find(), as long as it isn't hospitalList.end(), they can just call remove(). Moreover, if the iterator is at the end of the list, then OP can conclude that record isn't in hospitalList.
for the second link how can i merge my condition if(common.getPatientName() == str){

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.