# Txt file and data storage

Show 80 post(s) from this thread on one page
Page 4 of 4 First 1234
• 06-23-2008
dwks
Quote:

If i'm right the repeating of numbers occurs because i never clear my pairs vector so those old coordinates remain in the vector. How can i solve this?
The easiest way that I can think of? Declare pairs at the beginning of the while loop, so that a new variable is constructed for each iteration of the loop. :)

I just noticed that you posted your input file a while back. I'm testing your program now . . . .

 By doing that, and by changing the reference to pairs after the while loop, your program works correctly, at least I think it does. :)
Code:

```#include <iostream> #include <cstdlib> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; class Coord {          //values of coordinates (x,y)         public:                 double xt;                 double yt;                                 Coord(){};                 Coord(double x, double y) : xt(x), yt(y) {};                         };         typedef vector<Coord>Dots;              //collection of coordinates for one path (one line in txt file is one path) typedef vector<Dots>Paths;              //collection of all paths int main(int argc, char* argv[]) {                 Paths trajectories;                 int i,j;                 if (argc == 2) {                        ifstream dat (argv[1]);         if (!dat) {                 cerr << "Given file cannot be opened "<< argv[1]<<endl;         }                 while (!dat.eof()) {                 Dots pairs;                 string tmp;                 double x,y;                                 getline(dat, tmp);                 stringstream stmp(tmp);                 int p=-1;                                 while (stmp >> x >> y) {                                                                 p++;                                 cout <<"counter pp="<<p<<"\n"<<"number x      "<<x<<" number y        "<<y<<endl;                                                         pairs.push_back(Coord(x,y));                                 cout <<"number x inside vector  "<<pairs[p].xt<<"      number y inside vector  "<<pairs[p].yt<<endl;                                 // the lines for printing are just for testing because i had some problems here                 }                                 trajectories.push_back(pairs);          }      }      else if (argc < 2) {                 cout << "Error while running program" <<endl;                 cout << "Usage: eg_decomposition <file_name>" <<endl;         } else {                 cerr << "Too much parameters" << endl;                 exit (1);         }                      // printing of all paths                for(i=0; i < trajectories.size(); i++) {         for (j=0; j < trajectories[i].size(); j++)                 cout << '\t' << trajectories[i][j].xt <<' '<<trajectories[i][j].yt << endl;                                         }         return 0; }```
You'll notice I removed pp. It's the same thing as p, after all. [/edit]
• 06-23-2008
dwks
Here's the code from my previous post modified slightly. It does exactly the same thing. Consider my changes suggestions. :)
Code:

```#include <iostream> #include <cstdlib> #include <fstream> #include <vector> #include <string> #include <sstream> using namespace std; class Coord {          //values of coordinates (x,y) public:         double xt;         double yt;                 Coord(){};         Coord(double x, double y) : xt(x), yt(y) {};         }; typedef vector<Coord>Dots;              //collection of coordinates for one path (one line in txt file is one path) typedef vector<Dots>Paths;              //collection of all paths int main(int argc, char* argv[]) {         Paths trajectories;                 if (argc == 2) {                 ifstream dat (argv[1]);                 if (!dat) {                         cerr << "Given file cannot be opened "<< argv[1]<<endl;                 }                         string line;                 while(getline(dat, line)) {                         stringstream stmp(line);                         Dots pairs;                         double x,y;                                                 while (stmp >> x >> y) {                                 pairs.push_back(Coord(x,y));                         }                                                 trajectories.push_back(pairs);                 }         }         else if (argc < 2) {                 cout << "Error while running program" <<endl;                 cout << "Usage: eg_decomposition <file_name>" <<endl;         }         else {                 cerr << "Too much parameters" << endl;                 exit (1);         }                 // printing of all paths         for(Paths::size_type i=0; i < trajectories.size(); i++) {                 for (Dots::size_type j=0; j < trajectories[i].size(); j++)                         cout << '\t' << trajectories[i][j].xt <<' '<<trajectories[i][j].yt << endl;                         }                 return 0; }```
A brief overview of my changes:
• I indented your code a little bit more consistently, trying to stick with your indentation style.
• I removed the p variable, because you don't need it when you're just printing stuff. (Besides, it's the same thing as pairs.size().)
• I used size_types for the loop control variables because otherwise you have a signed/unsigned type mismatch. Basically, vector::size() returns an unsigned integer, and int is a signed integer; they have different ranges, and this could cause problems if you had [i]extremely[i] large vectors. You could also just cast the .size() calls to (int), or ignore this altogether. :)
• I removed the while(!dat.eof()) call. That can cause problems; see this: http://faq.cprogramming.com/cgi-bin/...&id=1043284351

 Oh, and I syntax highlighted the code with codeform. :) [/edit]
• 06-23-2008
Dwks thank you very much, this is great. I have only one more thing to do. Now i must calculate Euclidean distance for all paths in trajectories vector. Sort them and find the shortest path so i can send the robot down that path. I've solved the robot motion part of course but how do you suggest that i do the calculation and finding of the shortest path? Once again thank you very much for all your help.
By the way, you've done very good job on codeform ;)
• 06-23-2008
dwks
For the distances, use Pythagoras's theorem.
Code:

```#include <cmath> double hypotenuse(double x, double y) {     return std::sqrt(x*x + y*y); }```
(C99 has a hypot() function that does this. C++ may, you might want to try it.)

I'm not sure which pairs of points you want to examine, but finding the shortest path is pretty easy. It should just be a matter of finding the lowest number, no?

Let me know if you have trouble with that . . . .

 Note that you'll have to pass a hypot()-like function with the differences between coordinates, like this:
Code:

```Coord one(4, 5), two(6, 8); double distance = hypotenuse(one.xt - two.xt, one.yt - two.yt);```
[/edit]
• 06-23-2008
You can see the formula i need to use on this link:
http://en.wikipedia.org/wiki/Euclidean_metric.
I thought to calculate distance for all paths and store these values in vector of doubles. Then to sort them in some way. But how could i know to which path does the smallest number (shortest distance) belong to. Should i calculate those values for all paths again and compare them with the one that is the shortest?
I should pass values from the trajectories vector while calculating distances.
• 06-23-2008
dwks
If all you're trying to do is figure out the shortest distance, then you don't need to record every distance and then sort them. You can just calculate one distance; if it's the shortest distance found so far, then record the set of points you used to calculate this distance and the actual distance.

Something like this.
Code:

```        double value, min_value = distance(trajectories[0]);         Paths::size_type min_index = 0;                 for(Paths::size_type i=1; i < trajectories.size(); i++) {                 value = distance(trajectories[x]);                 if(value < min_value) {                         min_value = value;                         min_index = i;                 }         }                 // now trajectories[min_index] has the minimum Euclidean distace,         // the exact value of which is recorded in min_value.```
You'll have to write the distance() function, of course. :) That should be pretty easy, though.

 Just use a for loop to loop through all the items in a Paths. Calculate (x1-x2)**2 for each pair of coordinates, as your Wiki page indicates, sum those values, and return the square root of the final sum . . . . [/edit]

Best of luck, I'm off. :)
• 06-23-2008
```double distance (Paths path[i]) {         double dist, tmp;                 for (Dots::size_type j=0; j<path[i].size[];j++) {                         tmp += pow((path[i][j].xt - path[i][++j].xt),2) + pow((path[i][j].yt - path[i][++j].yt),2);         }         dist = sqrt(tmp);         return dist; }```