### Assigning Variables to Pulled Data

Basically what I'm trying to do is take coordinate data from a txt file and assign variables to them so I can later calculate distances between those coordinates and a set location (whose data is constant). The txt file looks something like this:
45.75 21.98
23.55 61.23
88.21 25.71
etc.
Each row is a set of coordinates with a space between latitude and longitude. I need to store each latitude and longitude separately. I have no clue how to do this, as I'm just getting started with c++. If it helps, the txt file is named "CoordinateData.txt."
Any help would be much appreciated.
why don't you start by reading the file.
http://www.cplusplus.com/doc/tutorial/files/
I have, it doesn't say how to do this.
it doesn't say how to do this!!!!!!!
Really??????????? I'm guesing the below code was just for show. Check the comments I made for you and start coding.

 1234567891011121314151617181920212223 // reading a text file #include #include #include using namespace std; int main () { string line; ifstream myfile ("example.txt"); //<-----------Try your file name(CoordinateData.txt) if (myfile.is_open()) { while ( myfile.good() ) { getline (myfile,line); cout << line << endl; } myfile.close(); } else cout << "Unable to open file"; return 0; }
You will probably want to create a vector of structs.
 12345678910111213 struct coord { double lat; double lng; }; coord co; vector v_coord; ... while ( myfile.good() ) { myfile >> co.lat >> co.lng; v_coord.push_back (co); }
Actually Tertius, it does NOT say how to do that. I'm not asking how to print out the results of the txt, as you kindly copy and pasted for me, but to assign variables to that data so I could later perform arithmetic on it.
An old code of mine! Am too lazy to go through the hole lot so I just cast the string to the vector. hope it helps

 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566 #include #include #include #include #include using namespace std; vector split(const string& s); int main () { string line; ifstream myfile ("CoordinateData.txt"); if (myfile.is_open()) { string sentence = ""; while (getline (myfile, line)) { sentence += line; } vector str_split = split(sentence); vector num; string one; for(vector::size_type i = 0; i != str_split.size(); ++i) { num.push_back(atoi(str_split[i].c_str())); } cout << sentence; myfile.close(); } else cout << "Unable to open file"; return 0; } vector split(const string& sentence) { vector ret; typedef string::size_type string_size; string_size i = 0; while(i != sentence.size()) { while(i != sentence.size() && isspace(sentence[i])) ++i; string_size j = i; while(j != sentence.size() && !isspace(sentence[j])) ++j; //Okay now what happens if we do find a string (lets copy it all) if(i != j) { ret.push_back(sentence.substr(i, j - i)); i=j; } } return ret; }
Topic archived. No new replies allowed.