Reputation: 1
I'm trying to create a program that has the user select a starting location from a list of vectors, select a destination from that same list using vec.push_back()
to use the same list minus 1 number. The code should then take the latitude and longitude from those locations and get the distance and flight time between them.
Entering the numbers works fine, but after I enter them the terminal states:
terminate called after throwing an instance of 'std::invalid_argument'
what(): stod
bash: line 12: 19106 Aborte
Here is my code:
#include<tgmath.h>
#include<stdio.h>
#include<vector>
#include<string>
#include<iostream>
using namespace std;
#define JET_SPEED 518 //Closest to middle between min and max
#define RADIUS 3958.8 //Radius of Earth doesn't change
double haversine(double lat1,double long1,double lat2,double long2)
{
double d = sqrt(pow(sin((lat2-lat1)/2),2)+(cos(lat1)*cos(lat2)*(pow(sin((long2-long1)/2),2))));
//Per the final simplified equation
return 2*RADIUS*asin(d);
}
double getFlightTime(double lat1,double lat2,double long1,double long2)
{
double d = haversine(lat1,long1,lat2,long2);
return (double)d/JET_SPEED;
}
int main()
{
vector<string> vec;
vec.push_back("Miami Beach, FL, USA 27.793449 -80.139198");
vec.push_back("Fargo, ND, USA 46.877186 -96.789803");
vec.push_back("Idaho city, ID, USA 43.828850 -115.837860");
vec.push_back("Zion, IL, USA 42.450806 -87.845978");
vec.push_back("Cairo, IL, USA 37.007828 -89.184265");
vec.push_back("Lewes, DE, USA 38.781082 -75.157150");
vec.push_back("Danville, IL, USA 40.144653 -87.634216");
vec.push_back("Toccoa, GA, USA 34.578903 -83.331581");
vec.push_back("Venice, FL, USA 27.109644 -82.448792");
vec.push_back("Torrington, CT, USA 41.806595 -73.130592");
cout<<"Select departure location from the following options" << endl;
int num=1;
int inputInteger=1;
for(auto tp:vec)
{
cout<<num<<". "<<tp<<endl;num+=1;
}
cin>>inputInteger;
string dep = vec[inputInteger-1];
vec.erase(vec.begin()+inputInteger-1);
cout<<"Select destination location from the following options" <<endl;
num=1;
for(auto tp:vec)
{
cout<<num<<". "<<tp<<endl;num+=1;
}
cin>>inputInteger;
string ar = vec[inputInteger-1];
stringstream tkn(dep);
string temp;
num=0;
double lat1,long1,long2,lat2;
vector<string> tp1,tp2;
while(getline(tkn,temp,' '))
{
tp1.push_back(temp);
}
lat1 = stod(tp1[tp1.size()-2]);
long1 = stod(tp1[tp1.size()-1]);
stringstream tkn2(ar);
num=0;
while(getline(tkn2,temp,' '))
{
tp2.push_back(temp);
}
lat2 = stod(tp2[tp2.size()-2]);
long2 = stod(tp2[tp2.size()-1]);
cout<<"\nThe haversine distance is : "<<haversine(lat1,long1,lat2,long2)<<"\n";
cout<<"\nTime take to reach the destination : "<<getFlightTime(lat1,long1,lat2,long2)<<"\n";
}
Upvotes: 0
Views: 1600
Reputation: 1
From std::stod()
's documentation,
If no conversion could be performed, an
invalid_argument
exception is thrown.
This means that the argument you're passing to the call to std::stod()
is not valid for the conversion to happen, and thus it throws an invalid_argument
exception, which you are not catching.
To solve this, make sure the argument to std::stod()
is valid, or catch the exception.
Upvotes: 1