I've got some input like this: (50.1003781N, 14.3925125E)
which is stored in const string & input
. What I'm trying to do is to extract the brackets, get the number representing the GPS coordination - 50.1003781, save N and do the same for the second coord.
So here goes my code:
istringstream iStream(input);
char brackets[2]; //To store the brackets
char types[2]; //To store if it is N/S or W/E
char delim; //To store ","
double degrees[2];
iStream >> brackets[0] >> degrees[0] >> types[0] >> delim;
//The delim loads the "," and here goes my problem with whitespace which is next
iStream >> degrees[1] >> types[1] >> brackets[1];
But it fails on loading degrees[1]
, it loads zero and tellg()
says -1 probably because of whitespace after the comma. The parsed string should be like this:
cout << brackets[0]; // "("
cout << degrees[0]; // "50.1003781"
cout << types[0]; // "N"
cout << delim; // ","
cout << degrees[1]; // "14.3925125"
cout << types[1]; // "E"
cout << brackets[1]; // ")"
I've tried skipws and noskipws but with no effect. Can anybody help, please? Thanks a lot.
The problem is pretty subtle, but it has to do with the fact that the E
in 14.3925125E
is parsed in scientific notation. In math, the E
suffix is an abbreviation for multiplying its operand by the exponentiation of 10
times the number that follows. For example, 5.23E4
in scientific notation means 5.23 * 10^4
.
In your situation, since a number doesn't follow the E
suffix, its parsed as an invalid floating point literal, so the number is not assigned to degrees[1]
.
Instead of reading the value into degrees[1]
directly, I would recommend instead reading it into a string, parsing the floating-point part from the E
, and convert and assign them to their respective variables.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With