I'm trying to make multiple arrays of maximum size 10 that hold the contents of an input file. Suppose the file is formatted like:
1 3 2 4 0 5 8 -3
3 1 4 6 0 3
4 83 2 12 1 3
4 30 4 -2
Where each line cannot exceed more than 10 integers.
void read_input(ifstream &file)
{
vector<array<int, 10> > main_array;
while(!(file.eof()))
{
int idx = 0;
string values;
array<int, 10> part_array;
getline(file, values);
istringstream inStream;
inStream.str(values);
while((inStream >> part_array[idx++]).good() == true);
main_array.push_back(part_array);
file.ignore();
}
for(auto it = main_array.begin(); it != main_array.end(); it++)
{
for(auto& s: *it)
{
cout << s << " ";
}
cout << "\n";
}
}
This gives me the output:
1 3 2 4 0 5 8 -3 0 6164240
1 4 6 0 3 5 8 -3 0 6164240
83 2 12 1 3 5 8 -3 0 6164240
30 4 -2 1 3 5 8 -3 0 6164240
Which is clearly wrong. I know this may have to do with me iterating over array elements that aren't initialized, but how can I avoid this error? Just as a note, it is essentially that I use arrays to hold the values at each line in the file. That is, one array must handle the integers in the first line; another in the second line...
while(!(file.eof()))is broken - movestring values;before and change towhile (getline(file, values))....ignorethemfile.ignore();