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.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?
I just noticed that you posted your input file a while back. I'm testing your program now . . . .
[edit] By doing that, and by changing the reference to pairs after the while loop, your program works correctly, at least I think it does.
You'll notice I removed pp. It's the same thing as p, after all. [/edit]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; }



LinkBack URL
About LinkBacks



