What im trying to achieve is to read in data from a .txt
file line by line, using getline()
, and save it as a string to the variable inVal
. I then want to save each individual number that is in the string to an individual element in the objects array by passing it to the member function ArrayBag.add(value)
. So far I've been able to read in data to inVal
fine but nothing I've tried has been able to convert and save the numbers in the string, including the code below after getline()
. Please any guidance or tips would be much appreciated.
the .txt
file looks like this:
3 4 5 7 5 16 7 12 11 12 3 9 9 8 1 12
15 4 3 6 1 12 3 12 7 8 19 9 11 12 8 5 -4 -100
My code that ive written so far is like this:
void readInv(ArrayBag &ArrayBag1, ArrayBag &ArrayBag2) {
//ArrayBag1 and ArrayBag2 are objects of class ArrayBag
std::string inVal;
//value to hold each line in file
std::ifstream readFile;
readFile.open("setInventory.txt"); //"setInventory.txt" is the txt file being read from.
if (readFile.is_open()) {
std::cout << "File is being read." << std::endl;
while(!readFile.eof()) {
getline(readFile, inVal);
for(int i = 0; i < inVal.size(); i++) {
std::cout << inVal[i] << std::endl;
ArrayBag1.add(inVal[i] - '0');
//ArrayBag1.add() is the public member function used to add the
//passing value to the private member array.
}
}
}
}