I'm attempting to read a file into a multidimensional vector, however the program is receiving a 'vector subscript out of range' error upon reaching the last set of data within the file.
The file is formatted as such:
[index between 1-10000][space][rand int between 1-100]
However each index can occur anywhere between 1-50 times, so the file ends up looking something like this:
And everything in-between (I re-wrote the data to be shorter, but the basis is the same).
Here is the code:
ifstream file;
int frequentCatcher[1000];
int numDistItems = 10000;
vector< vector<int> > initialVec(numDistItems, vector<int>(50));
file.open("T25.N0.1K.D10K.txt");
for (int i = 0; i <= 999; i++) //This loop sets all cells to 0 to begin.
{
frequentCatcher[i] = 0;
}
if (file.good())
{
int tracId = 1, tracNum = 0; //counters to track position in file
int id, num; //tracks actual data contained in file
while (!file.eof()) //traverse file
{
file >> id; //input from file
file >> num;
if (tracId == id)
{
initialVec[tracId][tracNum] = num;
tracNum++;
}
else
{
tracId++;
tracNum = 0; //reset item number each time a new transaction occurs
initialVec[tracId][tracNum] = num;
tracNum++;
}
}
The error occurs at the moment the id turns to 10000, the vector error is thrown and program crashes. This is after tracNum has been reset to 0, however the file still needs to read all values of 10000 and save them before proceeding.