I am using a ifstream to read through a file and am having trouble with the syntax of adding a struct to an array of structs
array of structs defined as:
storm_event* tbl;
tbl = new storm_event[fileLines];
"filelines" is the number of lines in the .csv I am reading from
adding in the values:
while (ip.good() || !ip.eof()) {
// get the info from each line
storm_event * storm = new storm_event{};
getline(ip,storm->event_id, ',');
getline(ip, storm->state, ',');
getline(ip, storm->year, ',');
getline(ip, storm->month_name, ',');
getline(ip, storm->event_type, ',');
getline(ip, storm->cz_type, ',');
getline(ip, storm->cz_name, ',');
getline(ip, storm->injuries_direct, ',');
getline(ip, storm->injuries_indirect, ',');
getline(ip, storm->deaths_direct, ',');
getline(ip, storm->deaths_indirect, ',');
getline(ip, storm->damage_property, ',');
getline(ip, storm->damage_crops, ',');
getline(ip, storm->tor_f_scale, '\n');
// add this to the storm event array
tbl[counter] = * storm;
cout << tbl[counter].state << endl;
cout << counter << endl;
counter++;
cout << counter << endl;
}
I am using a counter to add them into the array, but the loop only runs once, then reads all the getaline() lines one more time and then crashes.
I think the problem is this line:
storm_event * storm = new storm_event{};
How do I properly initialize a new struct and add it to a pointer array and create a new struct in the next iteration?
struct is already defined in a separate header file. Code also must be compatible with C++11, which IIRC requires different type of struct initialization.
Aucun commentaire:
Enregistrer un commentaire