I have the following code, that I'm using to reorganize some data in a simulation I'm working on. Essentially, I read in a list of components, and then reorganize them into a tensor, where the indexes of the tensor correspond to a spatial position.
The first time I read in the file, just to count the number of lines
int k1 = -1;    
ifstream fin;
fin.open("C:\\Users\\Chelsea\\Documents\\Visual Studio 2015\\Projects\\Full Spin Tracking\\Full Spin Tracking\\Fields\\RoundField1cmGrid.txt"); // open a file
if (!fin.good())
    return 1; // exit if file not found
              //Count lines in file
while (fin.good()) {
    getline(fin, line);
    k1++;
    if (k1 >= 270005) { break; }
}
fin.clear();
fin.seekg(0, fin.beg);  // reset the position of the file to the beginning.
cout << k1 << endl;
Then I set up arrays to hold data, and assign data to these arrays
//Initialize new arrays to hold static field info
double *xx = new (nothrow) double[k1];
double *yy = new (nothrow) double[k1];
double *zz = new (nothrow) double[k1];
double *bbx = new (nothrow) double[k1];
double *bby = new (nothrow) double[k1];
double *bbz = new (nothrow) double[k1];
double *bbmag = new (nothrow) double[k1];
//Write file static field data to 1D arrays
while (fin.good()) {
    data = line.find('\t');
    x1 = stof(line.substr(0, data));
    xx[j1] = x1;
    data2 = line.find('\t', data + 1);
    y1 = stof(line.substr(data + 1, data2));
    yy[j1] = y1;
    data3 = line.find('\t', data2 + 1);
    z1 = stof(line.substr(data2 + 1, data3));
    zz[j1] = z1;
    data4 = line.find('\t', data3 + 1);
    bx1 = stof(line.substr(data3 + 1, data4));
    bbx[j1] = bx1;
    data5 = line.find('\t', data4 + 1);
    by1 = stof(line.substr(data4 + 1, data5));
    bby[j1] = by1;
    data6 = line.find('\t', data5 + 1);
    bz1 = stof(line.substr(data5 + 1, data6));
    bbz[j1] = bz1;
    bmag = stof(line.substr(data6 + 1));
    bbmag[j1] = bmag;
    j1++;
    if (j1 == k1) { break; }
}//end while
fin.close();//Close file
Now, while I never use the integer k1 ever again, I find that if I place a watch on the variable, the value of k1 changes just before the file closes, and I can't figure out why, but I'm suspicious that something else is awry, and I want to make sure that this isn't going to screw up my program anywhere else.
What is causing this value to change?
