jeudi 4 février 2016

Infinite loop with string to int converter

I am currently coding a small programm which will be used to compute the distance between two cities given their geographical coordinates (latitude and longitude) (kind of the same way Google Maps does, I think) but only for a limited number of cities.

My program gets the city's name, latitude and longitude with a getline in a file text.

Then, I parse the line to get a)the name, b)the latitude (which is entered as angle.minutes.seconds), and c) the longitude. I parse latitude and longitude to have a vector with the 3 corresponding number but saved as strings.

Next step is to convert a vector of string into a vector of int. I've found a pretty neat method :

int strtoint(string str)
{
    int i=0;
    if(!(istringstream(str)>>i)) i=0;
    return i;
}

As for why I don't use stoi or atoi, I cannot get my compiler (MingW, in Code::Blocks 13.12) to understand them, so maybe I've missed a step.

However, when I try to use it with my vector, it goes into an infinite loop, and I don't understand why :

for(int unsigned i=0; i<latitude.size();i++)
{
    cout<<latitude[i]<<endl;
    i_latitude[i]=strtoint(latitude[i]);
}

Any help on the subject would be much appreciated.

Aucun commentaire:

Enregistrer un commentaire