0

I am trying to set a variable to the value of an element of a 2D array. This should be easy, but whenever I write it I get 0 in the variable. I read in a file to put in the array elements manually, which might be causing the problem, but I am not sure what's wrong.

Here is my code:

int main(){

//declaring all the variables
vector < vector <double> > data; // vector of vectors to hold the data.
int z=0;
int z1,z2;
//open the input file as an input file stream (ifstream)
    ifstream dataFile;
    dataFile.open( "GBplaces.csv" );

    //if the file is open...
    if (dataFile.is_open()) {

    // and while it's not the end of the file...

        while (!dataFile.eof() ){
            z++;
            if (z==1){
            string aLine;
            getline ( dataFile, aLine );
            printf ("place, type, population, lattitude, longitude \n\n");
            }

            else if(z==102){
            string aLine;
            getline ( dataFile, aLine );
            }

            else {

        //read in a line of the file and put the ontents into the arays.

        string aLine; // hold the read in line

        getline ( dataFile, aLine ); //reads line from dataFile to aLine

        //split up the string aLine based on where the comma is
        int comma;
        int nextCom=0;
        //comma_pos = aLine.find(',',0); //finds where the comma is, starting from the begining ie 0

        string xst, yst, zst; //temp variables for column
        vector <double> temp; // tempory vector for the points
        double xt, yt, zt;

        comma = aLine.find(',',0);   //find first position of comma
        xst = aLine.substr(0,comma);    // extracts string subvalue
        temp.push_back(atof(xst.c_str()));    //add value to end of temporary vector
        comma += 1; // add 1 to the comma position count

    while (aLine.find(',',comma) != -1) {
        // find middle values
        if(aLine==""){}
        else{
        nextCom = aLine.find(',',comma);
        yst = aLine.substr(comma, nextCom - comma);
        temp.push_back(atof(yst.c_str())); //convert string into double and add it to the end of the vector
        comma = nextCom + 1; 
        }
    }
    // same calculations as in the loop but for the last value after the comma 
    zst = aLine.substr(nextCom + 1, aLine.length()); 
    temp.push_back(atof(zst.c_str()));


        // push temporary vector onto data vector
        data.push_back(temp);       
        }
        }
    }

z = data[3][3].

When I run the program I get z=0, although when I print the table, the element is 51.26.

Dimitris
  • 49
  • 2
  • 10
  • This: `while (!dataFile.eof())` is almost assuredly a problem. [Read this for why](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong). That there is literally *no* error checking in any of this isn't exactly helping things much. – WhozCraig Dec 11 '13 at 00:07
  • Thanks a lot, will take a look at it! – Dimitris Dec 11 '13 at 00:09

1 Answers1

0

Update:

Found the error:

int z=0;
int z1,z2;

z1,z2 should be doubles instead of integers. This was messing up the rest of the number

Dimitris
  • 49
  • 2
  • 10