Reading two .dat files for calculating location distance I'm creating a program that reads data from two .dat files: one for food-bank location and the other for residence.  Right now, the program can calculate the distance between food-bank and residence and insert it into a distances vector.

My concerns are:


Is my logic right?
Can I determine the distance, in miles, between each location in residences and the closest food bank in foodbanks?  Note: the function will calculate in meters.




#include &lt;iostream&gt;
#include &lt;math.h&gt;
#include &lt;fstream&gt;
#include &lt;vector&gt;
#include&lt;iterator&gt;

using namespace std;

struct Foodbank
{
    double x;
    double y;


    //input stream overload
    friend std::istream &amp;operator&gt;&gt;(std::istream &amp;is, Foodbank &amp;f) {
        return is&gt;&gt;f.x&gt;&gt;f.y;
    }
    //output stream overload
        friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, Foodbank const &amp;f) {
            return os &lt;&lt; f.x  &lt;&lt; "\t"&lt;&lt; f.y&lt;&lt;"\t";

        }
};
struct Residence
{
    double x;
    double y;
    //input stream overload
    friend std::istream &amp;operator&gt;&gt;(std::istream &amp;is, Residence &amp;r) {
        return is&gt;&gt;r.x&gt;&gt;r.y;
    }
    //output stream overload
    friend std::ostream &amp;operator&lt;&lt;(std::ostream &amp;os, Foodbank const &amp;r) {
        return os &lt;&lt; r.x  &lt;&lt; "\t"&lt;&lt; r.y&lt;&lt;"\t";

    }
};

double distanceCalculate(double x1, double y1, double x2, double y2);
int main(int argc, const char * argv[])
{




    ifstream foodbankFile("foodbanks.dat");
    ifstream residenceFile("residences.dat");

    //vectors data
    vector&lt;Foodbank&gt; foodbankData;
    vector&lt;Residence&gt; residenceData;
    vector&lt;double&gt; distances;


    copy(std::istream_iterator&lt;Foodbank&gt;(foodbankFile),
         std::istream_iterator&lt;Foodbank&gt;(),
         std::back_inserter&lt;std::vector&lt;Foodbank&gt;&gt;(foodbankData));
      std::cout&lt;&lt;"read "&lt;&lt;foodbankData.size()&lt;&lt;" data: "&lt;&lt;std::endl;


    copy(std::istream_iterator&lt;Residence&gt;(residenceFile),
         std::istream_iterator&lt;Residence&gt;(),
         std::back_inserter&lt;std::vector&lt;Residence&gt;&gt;(residenceData));

    std::cout&lt;&lt;"read "&lt;&lt;residenceData.size()&lt;&lt;" data: "&lt;&lt;std::endl;
    for(auto r:residenceData)
    {
        for(auto f:foodbankData)
        {
            //std::cout&lt;&lt;f.x &lt;&lt;", "&lt;&lt;f.y &lt;&lt;", "&lt;&lt;std::endl;
            distances.push_back(distanceCalculate(f.x,f.y,r.x,r.y));
        }
        //std::cout&lt;&lt;r.x &lt;&lt;", "&lt;&lt;r.y &lt;&lt;", "&lt;&lt;std::endl;
    }

     std::cout&lt;&lt;"read "&lt;&lt;distances.size()&lt;&lt;" data: "&lt;&lt;std::endl;
    return 0;
}


// the distance in metres
double distanceCalculate(double x1, double y1, double x2, double y2)
{
    double x = x1 - x2;
    double y = y1 - y2;
    double dist;

    dist = pow(x,2)+pow(y,2);           //calculating distance by euclidean formula
    dist = sqrt(dist);                  //sqrt is function in math.h

    return dist;
}
Stop doing this:

using namespace std;


Yes every text book does this. But once you start writing programs more than 10 lines long it causes issues. Prefix items in the standard namespace with std:: (it was designed to be short). See detailed explanation here: Why is using namespace std considered bad practice

OK. What is the difference between Foodbank and Residence looks like they should simply be a Location class. If in later code you you add differences you can derive from the Location (or better use a location member (prefer composition over inheritance)).

Be consistent (and neat with your spacing).

return os &lt;&lt; r.x  &lt;&lt; "\t"&lt;&lt; r.y&lt;&lt;"\t";
//                     ^^^      ^^^      Hard to read

return os &lt;&lt; r.x  &lt;&lt; "\t" &lt;&lt; r.y &lt;&lt; "\t";


When initializing your arrays. No need to use std::copy. Just use the iterators in the vectors constructor.

vector&lt;Foodbank&gt; foodbankData;
copy(std::istream_iterator&lt;Foodbank&gt;(foodbankFile),
     std::istream_iterator&lt;Foodbank&gt;(),
     std::back_inserter&lt;std::vector&lt;Foodbank&gt;&gt;(foodbankData));


// Can be replaced by a single line:
vector&lt;Foodbank&gt; foodbankData(std::istream_iterator&lt;Foodbank&gt;(foodbankFile),
                              std::istream_iterator&lt;Foodbank&gt;());


Not sure why you are pushing the distance into a long single array. That does not help you work stuff out.

for(auto r:residenceData)
{
    for(auto f:foodbankData)
    {
        //std::cout&lt;&lt;f.x &lt;&lt;", "&lt;&lt;f.y &lt;&lt;", "&lt;&lt;std::endl;
        distances.push_back(distanceCalculate(f.x,f.y,r.x,r.y));
    }
}


I would suspect that what you want is a 2D array. The FoodBanks on the Y axis and the Residences on the X axis (as an example). Then given any Residences  you just need to scan the array vertically to find the closest FoodBank.

std::vector&lt;std::vector&lt;double&gt;&gt;   distance;
for(auto r:residenceData)
{
    distance.push_back(std::vector&lt;double&gt;());

    for(auto f:foodbankData)
    {
        //std::cout&lt;&lt;f.x &lt;&lt;", "&lt;&lt;f.y &lt;&lt;", "&lt;&lt;std::endl;
        distances.back().push_back(distanceCalculate(f.x,f.y,r.x,r.y));
    }
}


Wow thats a lot of parameters

double distanceCalculate(double x1, double y1, double x2, double y2)


If your FoodBank and Residence just inherited from a base of Location (or had a location member) then you can pass location as the start and end point and calculate the distance between them.