Feb 23, 2022 at 1:49am UTC
I have this code for reading a GPS file. It's running, but the output isn't correct. I really need help with the algorithm for the total distance traveled.
the user is asked to enter a file name, and this is the contents of the file:
1 2 3 4 5 6 7
START 0 0
DATA 24 24
DATA 4 24
DATA 4 124
DATA -25 12
DATA 0 0
STOP 195 215
this is the output:
1 2 3 4 5 6
Please Enter The Name Of The Data File: Data.txt
Final Location: (195.0, 215.0)
Total distance traveled 587.6
Distance to starting point 290.3
Average distance to start point = 83.4
my output
1 2 3 4
Final Location: (195.0,215.0)
Total Distance Traveled: 16346.0
Distance to starting point: 0.0
Average distance to start point = 0.0
This is my code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
#include <iostream>
#include <fstream>
#include<string>
#include<sstream>
#include<cmath>
#include <iomanip>
using namespace std;
int main()
{
string GpsFileName;
string gpsCommand;
string command;
string data;
double xCoord;
double yCoord;
double finLocationX;
double finLocationY;
double totDistTrav=0.0;;
double distStartPoint;
double avgDistStartPoint;
cout<<"Please Enter the Name of a Data File: " ;
cin>>GpsFileName;
cout<<GpsFileName<<endl;
ifstream openGpsfile(GpsFileName);
while (!openGpsfile)
{
cout<<"Error: File Not Open\n" ;
cout<<"Please Enter the Name of a Data File: " ;
cin>>GpsFileName;
cout<<GpsFileName<<"\n" ;
}
while (openGpsfile >> data >> xCoord >> yCoord)//repeat untill all the commands are processed
{
if (data.compare("START" ) == 0)
{
cout<<data<<" " <<xCoord<<" " <<yCoord<<endl;
}
else if (data.compare("DATA" ) == 0)
{
cout<<data<<" " <<xCoord<<" " <<yCoord<<endl;
totDistTrav += sqrt(pow(xCoord - yCoord, 2)) + pow(xCoord - yCoord,2);
}
else if (data.compare("STOP" ) == 0)
{
cout<<data<<" " <<xCoord<<" " <<yCoord<<endl;
finLocationX = xCoord;
finLocationY = yCoord;
}
}
cout<<fixed<<setprecision(1);
cout<<"Final Location: (" <<finLocationX<<"," <<finLocationY<<")" <<endl;
cout<<"Total Distance Traveled: " <<totDistTrav<<endl;
cout<<"Distance to starting point: " <<distStartPoint<<endl;
cout<<"Average distance to start point = " <<avgDistStartPoint<<endl;
openGpsfile.close();
return 0;
}
Last edited on Feb 23, 2022 at 1:49am UTC
Feb 23, 2022 at 2:11am UTC
you have borked your () on line 56.
Feb 23, 2022 at 3:01am UTC
Ok, thanks. So, how would I fix it?
Feb 23, 2022 at 3:59am UTC
what is the distance formula?
sqrt(dx*dx+dy*dy) ... a*a = b*b+c*c.. etc.
what do you have?
sqrt(dx*dx)+dy*dy
Last edited on Feb 23, 2022 at 4:00am UTC
Feb 23, 2022 at 5:16pm UTC
Here's your formula:
sqrt(dx*dx)+dy*dy
Here's the correct formula:
sqrt(dx*dx+dy*dy)
What do you need to change in:
totDistTrav += sqrt(pow(xCoord - yCoord, 2)) + pow(xCoord - yCoord,2);
?
Feb 23, 2022 at 5:20pm UTC
Folks, the guy has written
totDistTrav += sqrt(pow(xCoord - yCoord , 2)) + pow(xCoord - yCoord ,2);
That's going to take a hell of a lot more to fix than just shifting a right bracket.
Subtract your PREVIOUS point and use the hypot() function.
Last edited on Feb 23, 2022 at 5:22pm UTC