Issue with a function that doesn't execute inside of another function.

I'm writing a program that takes in three strings ie. 123.45/N 23.44/S Location Name, then the user inputs the number of locations they want to compare the distance to, and finally each location, ie.

1
2
3
4
5
12.23/N 22.22/E LAX
3
15.55/S 23.444/W Brazil Airport
120.44/N 99.54/E Mexico Airport
99.22/N 44.4/W Unnamed Airport


Then it prints out the closest distance (in miles) and the distance farthest away.

I've been able to parse the numbers out of the first two strings inputted, and in a separate method which takes the number of lines to input as a parameter, goes through a loop asking for input, and runs two methods that return a value to a variable outside of the function: the farthest away location and the closest location to the starting point.

Here is the method:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
void getTotalLocations(int amountInput) {
    std::string inputLat, inputLong, inputLoc;
    double inputLatParse, inputLongParse;
    double curLocation;
    // ask user for locations
    for(int i = 0; i < amountInput; i++) {
        // grab input
        std::cin >> inputLat >> inputLong;
        getline(std::cin, inputLoc);
        // parse it
        inputLatParse = numberParse(inputLat);
        inputLongParse = numberParse(inputLong);
        // calculate how far the location is from the starting location
        curLocation = getHaversine(inputLatParse, inputLongParse);
        curLocation = getHaversine(inputLatParse, inputLongParse);
        // find and store the closest and farthest location
        getFarthestTotal(curLocation, inputLat, inputLong, inputLoc);
        getClosestTotal(curLocation, inputLat, inputLong, inputLoc);
    } // end for
}


And my getFarthestTotal and getClosestTotal:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// get the farthest total
double getFarthestTotal(double curFarthestLoc, std::string &farthestLatInput, std::string &farthestLongInput, std::string &farthestLocInput) {
    if(curFarthestLoc > farthestLocTotal) {
        farthestLocTotal = curFarthestLoc;
        // save the farthest location into strings so we can display them later.
        farthestLatitude = farthestLatInput;
        farthestLongitude = farthestLongInput;
        farthestLocation = farthestLocInput;
    }
    return farthestLocTotal;
}

// get the closest total
double getClosestTotal(double curClosestLoc, std::string &closeLatInput, std::string &closeLongInput, std::string &closeLocInput) {
    if(curClosestLoc < closeLocTotal) {
        closeLocTotal = curClosestLoc;
        // save input user entered
        closeLatitude = closeLatInput;
        closeLongitude = closeLongInput;
        closeLocation = closeLocInput;
    }
    return closeLocTotal;
}


The problem I'm having is that my function, getTotalLocation completely stops after running getFarthestTotal(), so it never runs getClosestTotal() and therefore I never have any numbers to display afterwards. As far as I know, the function shouldn't just stop after returning one thing, but that's what it seems to do (after testing with print statements).

My display ends up looking like this (also with weird formatting):

1
2
3
Start Location: 12.23/N 22.22/E ( LAX)
Closest Location:  () (0.0 miles)
Farthest Location: 99.22/N 44.4/W ( Unnamed Airport) (125.3 miles)


And my display method looks like this:

1
2
3
4
5
void displayOutput() {
    std::cout << "Start Location: " << startLatitude << " " << startLongitude << " (" << startLocation << ")" << std::endl;
    std::cout << "Closest Location: " << closeLatitude << " " << closeLongitude << " (" << closeLocation << ") (" << closeLocTotal << " miles) " << std::endl;
    std::cout << "Farthest Location: " << farthestLatitude << " " << farthestLongitude << " (" << farthestLocation << ") (" << farthestLocTotal << " miles)" << std::endl;
}


So basically, what is going wrong with my getClosest function that is causing it to not load any values or print them out?
Not sure.
Lines 14 and 15 of void getTotalLocations(int amountInput)are identical.

Did you try following the code in a debugger ?
print statements are useful but nothing beats a good debugger's find-needle-in-haystack ability.

I would make sure those streams are doing what they should be to start.
I don't see any error checking.
Last edited on
Topic archived. No new replies allowed.