hi guys...ok i have a .txt file like this:
city adelaide 138.0 145.0
city canberra 149.0 145.0
city hobart 142.0 137.0
conn adelaide canberra
conn adelaide hobart
start sydney
goal hobart
you get the structure...the numbers are the x,y coordinates of the cities...and conn indicated which cities are connected together...
i need to read in all this and make a node for each city...save its corresponding info. into the node...i can read the file...my code is give below...im using a vector of nodes to save all the nodes generated...could someone help me correct this...i get runtime errors...ive just tried the first part which is making nodes corresponding to cities and storing their x, y valyes...
This is my node class:
Code:class Node { public: Node *parent; float g; // cost of this node + its parents float h; // heuristic estimate float f; // g + h string CityName; int x,y; }; //this is the readfile function i have so far: //Reads the contents of the input file void readFile() { char check[6]; int counter1 = 0; int counter2 = 0; float cords[20]; vector <string> mystring(200); vector <Astar::Node *> CityNode; //Node is in Astar class ifstream fin ("inputfile.txt"); while (! fin.eof() ) { fin >> check; if (strcmp(check, "city") == 0) { fin >> mystring[counter1]; CityNode[0] -> CityName = mystring[counter1]; counter1++; fin >> cords[counter2]; CityNode[counter1] -> x = cords[counter2]; counter2++; fin >> cords[counter2]; CityNode[counter1] -> y = cords[counter2]; counter2++; counter1++; } if (strcmp(check, "conn") == 0) { fin >> mystring[counter1]; counter1++; fin >> mystring[counter1]; counter1++; } if (strcmp (check, "start") ==0) { fin >> mystring[counter1]; counter1++; } if (strcmp (check, "goal") ==0) { fin >> mystring[counter1]; counter1++; } } //to test the vector contents for (int i = 0; i<counter1; i++) { Form1->ListBox1->Items->Add((CityNode[counter1] -> CityName).c_str()); } return; }
Error: When I run it, it says access violation at some address...i think it's because of the line:
Code:CityNode[0] -> CityName = mystring[counter1];



LinkBack URL
About LinkBacks


