Home > Net >  Why does it show terminate called after throwing an instance of 'std::logic_error' what():
Why does it show terminate called after throwing an instance of 'std::logic_error' what():

Time:01-22

At first, I thought there is an error when I was using the while loop so I've tried using for loop but it still displays the same error. I have tried to look up the reason why this error happened but I am still unable to figure out which line in these codes caused the error.

int main()
{
    ifstream patientData;
    string name;
    int count = 0;
    patientData.open("PatientList.txt");

    string patient_name[NUM_PATIENTS] = {0};
    double ward[NUM_PATIENTS]         = {0};
    double surgery[NUM_PATIENTS]      = {0};
    double medication[NUM_PATIENTS]   = {0};
    double service[NUM_PATIENTS]      = {0};
    double totalCharge[NUM_PATIENTS]  = {0};

    while(count < NUM_PATIENTS)
    {
        getline(patientData, patient_name[count], '\t');        
        patientData >> patient_name[count];
        patientData >> surgery[count];
        patientData >> medication[count];
        patientData >> service[count];
        patientData >> totalCharge[count];
        count  ;
    }

    patientData.close();
    return 0;
}

I apologize in advance if this sounds like a stupid question. Currently, I'm following the guide from a textbook named "Starting Out with C From Control Structures through Objects Ninth Edition by Tony Gaddis" Can someone help me?

CodePudding user response:

In short: You are trying to initialize your arrays with 0, which is a nullptr. So you are actually trying to initialize your first string in the array with a nullptr.

Let's examine

string patient_name[NUM_PATIENTS] = { 0 };
  • It defines an old style C array of NUM_PATIENT strings.
  • It initializes this array with { 0 }.
  • ... which means, the first string get's initalized with 0, the rest of the strings get default-initialized.
  • ... which means, the first string in the array is initialized thus: string(0), which is similar to string(nullptr) - which probably leads to your exception string::_S_construct null not valid

Actually all your attempts at initalizing are wrong.

You don't need to write = {0} for the patient_name[NUM_PATIENT] array at all, because the strings in this array are "default-initialized". They know how initialize themselves to an empty string. string patient_name[NUM_PATIENT]; is enough.

But your other arrays don't get properly initialized at all. = {0} will initialize the first element of your vector. The remaining elements are "default" initialized (for classes) or not initialized at all (for doubles).

Don't use patient_name[NUM_PATIENT] at all. Get a better textbook. This is an old-style C array. Use std::vector (best) or std::array (better).

Much better would be this:

   std::vector<string> patient_names;
   std::vector<double> medication...

   for (size_t i=0; i<NUM_PATIENT;   i) {
      string next_patient;
      getline(patients, next_patient);
      patient_names.emplace_back(next_patient);
...

or, to stick close to the text book:

   std::vector<string> patient_name(NUM_PATIENTS);
...
   while (count<NUM_PATIENTS) {
      getline(patients, patient_name[count]);
  •  Tags:  
  • Related