void initializeVectorFromFile(vector<SInventory> & inven){
ifstream initData("inventoryData.txt");
if(!initData){
cout << "File could not be accessed! Press any key to terminate program...";
_getch();
exit(1);
}
while(!initData.eof()){
SInventory item;
initData >> item.itemID;
getline(initData,item.itemName);
initData >> item.pOrdered
>> item.menufPrice
>> item.sellingPrice;
item.pInStore = item.pOrdered;
item.pSold = 0;
inven.push_back(item);
cout << item.itemID << endl;
}
cout << "File Read Success!" << endl;
initData.close();
}
The .txt file I am reading from contain data structured in this order:
int
string
int double double
The output which is in the last line of the while-loop is repeated as the first itemID within the file. The initData stream does not read subsequent entries in the .txt file.
1111
1111
1111
1111
1111
...
Don’t use
while (!initData.eof())— ever. It’s pretty much a guaranteed bug.I’d start with code to read a single
SInventoritem from the file:With that in place, it’s probably easiest to just do without the rest of the function, and just initialize the vector directly:
No loop, no mucked up test for EOF, etc., just a vector initialized from a pair of iterators.