Assignment:
-
Read in info from text file (done)
-
Retrieve only parts of text file using substr method (done)
-
Store info into instance variables (need help)
Here is the code I am having trouble with:
string* lati;
lati = new string(data.substr(0, data.find_first_of(",")));
double* latDub;
latDub = new double(atof((char *)lati));
this->latitude = *latDub;
-
I need to store the latitude into the instance variable
latitude. -
The variable
datais the read-in text file. -
this->latitudeis declared as adouble.
I have tested and the variable lati is the correct value, but once I try to convert it into a double the value changes to 0 for some reason. I am specifically supposed to use the atof method when converting!
(char *)latidoesn’t do what you think it does. What you’re clearly trying to do there is get thecharsequence associated with lati, but what you’re actually doing is just squeezing astring*into achar*which is all kinds of bad.There’s a member function on std::string that will give you exactly what you want. You should review the documentation for string, and replace
(char *)latiwith a call to that function.