Help with C++ GPS Program

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
you have borked your () on line 56.

Ok, thanks. So, how would I fix it?
So, how would I fix it?


un-bork it.
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
So, how would I fix it?


Move the right bracket..
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); ?
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
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
#include <iostream>
#include <iomanip>
#include <fstream>
#include <string>
#include <cmath>
using namespace std;

int main()
{
   string command;
   double x, y, xprev, yprev, xstart, ystart;

   ifstream in( "data.txt" );
   in >> command >> xstart >> ystart;
   xprev = xstart;   yprev = ystart;

   double totalDistance = 0, sumFromStart = 0;
   int N = 0;
   while ( in >> command >> x >> y )
   {
      totalDistance += hypot( x - xprev, y - yprev );
      sumFromStart += hypot( x - xstart, y - ystart );
      N++;
      xprev = x;   yprev = y;
   }

   cout << fixed << setprecision( 1 );
   cout << "Final location: (" << x << ", " << y << ")" << '\n';
   cout << "Total distance traveled: " << totalDistance << '\n';
   cout << "Distance to starting point: " << hypot( x - xstart, y - ystart ) << '\n';
   cout << "Average distance to start point = " << sumFromStart / N << '\n';
}


Final location: (195.0, 215.0)
Total distance traveled: 587.6
Distance to starting point: 290.3
Average distance to start point = 83.4

Topic archived. No new replies allowed.