So I've declared an array of pointers that look something like
Items* _items[ARRSIZE];
Basically with the goal of using them as an array of objects (one for meat, one for produce) which is dynamically decided on run time. I'm calling the following function in my constructor and I've identified it as the reason I keep segfaulting before the main function.
void Inventory::loadRecs(){
datafile.open(_filename);
int i = 0;
char c;
//create fileif it doesnt exist
if(datafile.fail()){
datafile.clear();
datafile.close();
datafile.open(_filename, ios::out);
datafile.close();
}
else{
//read from file
while(!datafile.eof()){
if(_items[i] != nullptr){
delete _items[i];
}
c = datafile.get();
if(c == 'P'){
_items[i] = new Produce;
}
if (c == 'M'){
_items[i] = new Meat;
}
datafile.ignore();
_items[i]->load(datafile);
i++;
datafile.ignore(); //ignore endl
}
_noOfItems = i;
datafile.close();
}
}
The text file I'm reading from is fairly straight forward reading something like
P,123,carrots,0.66,[NEWLINE] The first character identifies what kind of product it is (meat or produce) and the rest of the line is read in with the load function.
My Inventory class looks something like this:
class Inventory{
char _filename[256];
Item* _items[5];
std::fstream datafile;
int _noOfItems;
}
And the constructor just initializes everything and calls loadsRecs (which is where I get my segfault from)