Inner loop is executing as many times as there is line in regarding files. But outer loop is executing only once, regardless of there are other line presence in regarding files.I want to compare values from each line of first file (containing m lines) with each line of second file (containing n lines) . how to iterate loop m X n times ?

ifstream gpsfixinput, geofenceinput;     
gpsfixinput.open(GPSFIX_FILE, ios_base::in);
geofenceinput.open( GEOFENCE_FILE, ios_base::in);

string gline,lline ;
while(getline(gpsfixinput, lline))  
{
        istringstream lin(lline);
        double lat,lon;
        lin >> lat >> lon ; 

        while(getline(geofenceinput, gline))  
        {
            istringstream gin(gline);    


            double glat, glon, rad;
            string alert;


            gin >> glat >> glon >> rad >>alert;   
            distance(lat,lon, glat, glon , rad , alert );

        }
1

There are 1 best solutions below

1
On BEST ANSWER

Your outer loop is (probably) running for every line in your first file, but because you have read the whole of the second file, it isn't having any side effects.

If you want the cross product of (lat, lon) pairs and (glat, glon, rad, alert) quads, you should could read them in separate loops into some container, and then loop over your containers.

e.g.

struct gpsfix
{
    double lat, lon;
}

struct geofence
{
    double glat, glon, rad;
    string alert;
}

std::vector<gpsfix> gpsfixes;
std::vector<geofence> geofences;

while(getline(gpsfixinput, lline))  
{
    istringstream lin(lline);
    double lat, lon;
    lin >> lat >> lon ; 
    gpsfixes.emplace_back(lat, lon);
}

while(getline(geofenceinput, gline))  
{
    istringstream gin(gline);    

    double glat, glon, rad;
    string alert;

    gin >> glat >> glon >> rad >> alert;   
    geofences.emplace_back(glat, glon, rad, alert);
}

for (gpsfix fix : gpsfixes)
{
    for (geofence fence : geofences)
    {
        distance(fix.lat, fix.lon, fence.glat, fence.glon, fence.rad, fence.alert);
    }
}