0

I was trying to make a program parsing gps value($GPRMC).

First successfully parse the string from whole line.

$GPRMC,062513.000,A,3645.9487,N,12716.8382,E,1.76,295.08,160116,,,A*6E

After I did it, I used the function stod() for string to be double.

But it collapse if I debug.

code here.

#include<iostream>
#include<string>


using namespace std;


//"$GPRMC,062513.000,A,3645.9487,N,12716.8382,E,1.76,295.08,160116,,,A*6E";

int main()
{
    string gps="$GPRMC,062516.000,A,3645.9494,N,12716.8365,E,1.82,302.69,160116,,,A*63";

    int com_1;
    int com_2;
    int com_3;
    int com_4;
    int com_5;
    int com_6;
    int com_7;
    int com_8;
    int com_9;

    string kind;
    string time;
    string state;
    string latitude;
    string n_s;
    string longitude;
    string e_w;
    string knot;
    string degree;

    double Kind;
    double Time;

    double Latitude;

    double Longitude;

    double Knot;
    double Degree;

    com_1=gps.find(",");
    com_2=gps.find(",",com_1+1);
    com_3=gps.find(",",com_2+1);
    com_4=gps.find(",",com_3+1);
    com_5=gps.find(",",com_4+1);
    com_6=gps.find(",",com_5+1);
    com_7=gps.find(",",com_6+1);
    com_8=gps.find(",",com_7+1);
    com_9=gps.find(",",com_8+1);


    kind=gps.substr(0,com_1);
    time=gps.substr(com_1+1,com_2-com_1-1);
    //state=gps.substr(com_2+1,com_3-com_2-1);
    latitude=gps.substr(com_3+1,com_4-com_3-1);
    //n_s=gps.substr(com_4+1,com_5-com_4-1);
    longitude=gps.substr(com_5+1,com_6-com_5-1);
    //e_w=gps.substr(com_6+1,com_7-com_6-1);
    knot=gps.substr(com_7+1,com_8-com_7-1);
    degree=gps.substr(com_8+1,com_9-com_8-1);


    Kind=stod(kind);
    Time=stod(time);
    //State=stod(state);
    Latitude=stod(latitude);
    //N_s=stod(n_s);
    Longitude=stod(longitude);
    //E_w=stod(e_w);
    Knot=stod(knot);
    Degree=stod(degree);
NathanOliver
  • 171,901
  • 28
  • 288
  • 402
  • 1
    *But it collapse if I debug.* Can you explain that? – NathanOliver Jan 20 '16 at 16:05
  • Have you tried to debug your code? Step through it using a debugger, line by line, to see that it actually does what you expect it to, and that all variables are set as you expect them to? – Some programmer dude Jan 20 '16 at 16:09
  • 1
    Looks like you are trying to convert the text "$GPRMC" into an integer. I don't see any decimal digits in that string. – Thomas Matthews Jan 20 '16 at 16:12
  • 1
    By the way, floating point data types on computers are not really good enough to store coordinates received by a GPS, unless you are content with there being some slight errors. Same with the time. – Some programmer dude Jan 20 '16 at 16:17
  • [This](http://stackoverflow.com/questions/24504582/how-to-test-whether-stringstream-operator-has-parsed-a-bad-type-and-skip-it) might be helpful. – πάντα ῥεῖ Jan 20 '16 at 16:50

2 Answers2

1

In a review, let us consider how you parse the first datum.

   com_1=gps.find(",");

The above line finds the position of the first comma, good.

Next, you extract the substring of the first item:

kind=gps.substr(0,com_1);

The variable kind should be "$GPRMC", according to your input.

Finally, you convert this text to a double:

Kind=stod(kind);
// In other words, this is equivalent to  
// Kind = stod("$GPRMC");  

The stod function fails because there are no digits in the string.

BTW, differing variable names by case, such as kind vs. Kind, is considered as poor coding practices. Most coding guidelines forbid this and require variable names to differ by more than case.

Thomas Matthews
  • 56,849
  • 17
  • 98
  • 154
0
Kind=stod(kind);

does not seem right. The value of kind is going to be "$GPRMC". You cannot extract a double from it.

PS Fixing it may not fix any other problems you might have.

R Sahu
  • 204,454
  • 14
  • 159
  • 270