Map Program

I've been working on this for awhile now. I can't figure out what I'm doing wrong.


#include <iostream>
#include <string>
#include "calc_distacne.hpp"
#include <stdlib>
#include <cmath>

using namespace std;

int main()
{
string start_lat, start_lon, latitude_direction, longitude_direction, location_name;

getline(cin, start_lat, '/');
getline(cin, latitude_direction, ' ');
getline(cin, start_lon, '/');
getline(cin, longitutde_direction, ' ');
getline(cin, location_name);

double dstart_lat=atof(start_lat.c_str());
double dstart_lon=atof(start_lon.c_str());

double target_locations;
cin >> target_locations;

string xlatitude, xlongitude, xlat_d, xlong_d, xname, clslat, cname, farlat,
fname, clslon, farlon;
double xdlatitude=0, xdlongitude=0, cdistance=0, fdistance=0;

for (i=0; i<target_locations; i++){
double temp_dis;
getline(cin,xlatitude, '/');
getline(cin,xlat_d, ' ');
getline(cin,xlongitude, '/');
getline(cin,xlong_d, ' ');
getline(cin,xname);

xdlatitude=atof(xlatitude.c_str());
xdlongitude=atof(xlongitude.c_str());

if (xlat_d=='S') {
xdlatitude=xdlatitude * -1}
else (xlong_d=='W') {
xdlongitude= xdlongitude * -1}

double d2r(double deg) {
return (deg* 3.14/180);}

double calc(double lat1, double lat2, double lon1, double lon2){

double a = pow(sin(d2r(lat2-lat1)/2),2) + cos(d2r(lat1)) * cos(d2r(lat2)) * pow(sin(d2r(lon2-lon1)/2),2);

double c = 2 * atan2(sqrt(a), sqrt(1-a));

return 3959 * c
}
temp_dis=calc(dstart_lat, xlatitude, dstart_lon, xlongitude);

if (temp_dis<distance){
clslat=xlatitude;
clslon=xlongitude;
cdistance= temp_dis;
cname=xname;}
else (temp_dis>distance){
farlat=xlatitude;
farlon=xlongitude;
fdistance=temp_dis;
fname=xname;}
}

cout >> "Start Location: " >> start_lat >> start_lon >> location_name >> "Closest Location: " >> clslat >> clslon >> cname >> cdistance >> "Farthest Location: " >> farlat >> farlon >> fname >> fdistance << endl;

return 0;
}


The following is an example of the program's execution, as it should appear in the console. Boldfaced, italicized text indicates input, while normal text indicates ouptut.

33.9425/N 118.4081/W Los Angeles International Airport
3
20.8987/N 156.4305/W Kahului Airport
47.4647/N 8.5492/E Zurich Airport
23.4356/S 46.4731/W Sao Paolo-Guarulhos International Airport
Start Location: 33.9425/N 118.408/W (Los Angeles International Airport)
Closest Location: 20.8987/N 156.43/W (Kahului Airport) (2483.3 miles)
Farthest Location: 23.4356/S 46.4731/W (Sao Paolo-Guarulhos International Airport) (6164.9 miles)
Notice, again, that there are no prompts or other ouptut, other than the output that is required as specified above. This may seem strange, but it's safe to assume that this program is not ultimately intended to have a human user; we'll be running automated tests against it, so welcome prompts, etc., will get in the way of us determining whether your program is correct.
Last edited on
Haven't looked at anything else in the program except for noticing that it does a mixture of formatted input and unformatted input.

Read this first: http://www.cplusplus.com/forum/general/69685/#msg372532
Fix the program to make the stream discard trailing white space left in the input buffer by the formatted input operation. If there still are problems with the code that you can't resolve, post the modified code along with your questions.
Topic archived. No new replies allowed.