String Manipulation

closed account (18U4GNh0)
I have this function that reads from a file the output of a GPS.In the output file i only print the GPGLL output for longitude and latitude.I want to break the sentence of GPGLL and receive the numbers as string before processing them and turning them into double to use them as variables.

double Findlonglat()
{//findlonglat begins
ifstream GPGLLfile; //creating the file to read from
GPGLLfile.open("gpgllfile.txt"); //opening the text file
string output,line;
getline(GPGLLfile, line);//reading the line from the file
string longitude, latitude;//the variables to receive in as string
double longnumber;//the doubles
double latnumber;
stringstream input_stringstream(line);
if (!GPGLLfile)
{
return 0;
}


if (getline(input_stringstream, output, ','))
{
return 0;
}
if (getline(input_stringstream, latitude, ','))
{
latnumber=stod(latitude);
}
if (getline(input_stringstream, output, ','))
{
return 0;
}
if (getline(input_stringstream, longitude, ','))
{
longnumber=stod(longitude);
}
return latnumber,longnumber;
}

I know there is a problem in the way i break the sentence and there is surely an easier way but i cant figure it out.
Could You please discribe what exactly doesn't work. That would be helpfull
closed account (18U4GNh0)
i think the problem is the way i am handling the if statements.I think it does not even enter the if statements.
Please, edit your original post and put your indented code between [code]...your code here...[/code] tags.

One problem I see is this -> return latnumber,longnumber;. This doesn't do what you want; it only returns longnumber. It's not possible to return multiple results from a function this way in C++.

What you should probably do is:

1
2
3
4
5
bool Findlonglat(double & longnumber, double & latnumber) {
  // set longnumber and latnumber variables appropriately
  // return false if there was a problem
  // return true if everything was ok
}

If you need help parsing the file, you should at least post a small representative part of it (you can use [output]...file contents here...[/output] tags for this). We can't guess what the file looks like :P
Last edited on
Topic archived. No new replies allowed.