0

I am trying to read an excel file that four columns and four rows. First is city. Second is country. Third is latitude. Fourth is longitude. I was able to finally read them correctly and store them into variables thanks to another post I had. Then, I had to calculate the distance between those latitudes and longitudes and two other ones. Now, I am trying to put the distances into an array, so that I can access whatever index I want outside of the loop. Here is what I have:

while (!inFile.eof()) {
    getline(inFile, city, ',');
    getline(inFile, country, ',');
    getline(inFile, lat, ',');
    inFile >> lon;

    //Converts string latitude and longitude to int to be able to use later 
    double latitude;
    istringstream convert(lat);
    if (!(convert >> latitude)) {
    latitude = 0;
    }//else do nothing, initializing not needed

    double longitude;
    istringstream convert1(lon);
    if (!(convert1 >> longitude)) {
        longitude = 0;
    }//else do nothing, initializing not needed


    City d1;
    distanceArray[i] = d1.greatCircleDistance(latitude, longitude, lat1, lon1);



    //Display great distance for all cities

    cout << "Great distance between " << city << " and random city is " << d1.greatCircleDistance(latitude, longitude, lat1, lon1) << endl;
    i++;
}

    cout << distanceArray[1] << endl; 

I am trying to put the distance between lat1, lat 2 and longitude, latitude in distanceArray so that after the loop when I put cout << distanceArray[1], it gives me the second distance. But when I compile it, it tells me h.exe has stopped working. Why? I am so confused.

Jerry Coffin
  • 476,176
  • 80
  • 629
  • 1,111
PaliiBenet
  • 41
  • 1
  • 1
  • 3
  • you need to run it under a debugger, this will show you where it is dying. You dont say what platform you are on so I can't tell you how to do that – pm100 Oct 15 '15 at 22:17
  • You can't read an Excel file like this, you wrote code to read a text file. Big difference. – Hans Passant Oct 15 '15 at 22:18
  • i suspect he has a csv – pm100 Oct 15 '15 at 22:21
  • 1
    [while (!inFile.eof()) is the wrong way to go about this.](http://stackoverflow.com/questions/5605125/why-is-iostreameof-inside-a-loop-condition-considered-wrong) try `while (getline(inFile, city, ',') && getline(inFile, country, ',') && getline(inFile, lat, ',') && inFile >> lon) {` instead. It will perform all of your reads, test each one for success, and only enter the loop body if everything read correctly. – user4581301 Oct 15 '15 at 22:23
  • 1
    Also take a look at [`std::stod`](http://en.cppreference.com/w/cpp/string/basic_string/stof) as a replacement for the stringstreams – user4581301 Oct 15 '15 at 22:43
  • How `distance_array` is declared? – user58697 Oct 15 '15 at 23:22
  • If you don't know to operate a debugger you should learn for your own mental healths benefit. But if you can't then add a lot of `cout` statements and divide and conquer the code until you find the offending line. – Surt Oct 15 '15 at 23:45

0 Answers0