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.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
// reading a text file
#include <iostream>
#include <fstream>
#include <string>
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.
1
2
3
4
5
6
7
8
9
10
11
12
13
 
  struct coord
  { double lat; 
     double lng;
   };

  coord  co;
  vector<coord>  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

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <fstream>
#include <iostream>
#include <string>
#include <vector>
#include <cstdlib>

using namespace std;

vector<string> 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<string> str_split = split(sentence);
  	vector<double> num;
    
    string one;
    for(vector<string>::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<string> split(const string& sentence)
{
	vector<string> 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.