Original Post
Hi I'm having problems, which I think are down to pointers. I'm trying to read some values from a text file, store them in a vector, and then in another method read them from the vector. First I have a class called orc with the following attributes:- Below is the first method in the orc clas it should read the orc.data file and read in a direction which is W, E, N or S and a distance which could be 0-999. In this method I printed out the contents of the vector after asigning it, and it prints the values just fine. Next I have this method in Orc it begins by assigning the contents of the first part of a vector to mDirection. (mCurrentPos is set to 0 in the previous method). My problem is that if I do a printf("mDirection") it prints out garbage. From what I can see it's not assigning the contents of the vector, but some other value, I can't understand why it does this, when in the previous method (both methods of orc) that sort of assignment works fine. Any ideas?
private:
char *mDirection; //The current direction the sprite is facing
int mDistance; //How for the sprite has moved
int mCurrentPos; //Position to read from in array
int mLife;
vector<char *> mtheDirection;
vector<int> mtheDistance;
int orc::loadDirections()
{
FILE *fp;
char *filename="data/orc.data";
char buffer[255];
char dir;
int dis;
int count = 0;
if((fp=fopen(filename, "r")) == NULL)
{
printf("Error opening file %s\n", filename);
return -1;
}
while(!feof(fp))
{
fgets(buffer, 255, fp);
sscanf(buffer, "MV: %s %d", &dir, &dis);
mtheDirection.push_back(&dir);
mtheDistance.push_back(dis);
printf("DIR: %s\n", mtheDirection[count]);
printf("DIS: %d\n", mtheDistance[count]);
printf("COUNT %d\n", count);
count++;
}
mCurrentPos = 0; //Current Posistion in the array
mDistance =0; //Distance moved so far
mtheDirection.push_back("W");
mtheDirection.push_back("S");
mtheDistance.push_back(200);
mtheDistance.push_back(200);
return 0;
}
void orc::patrol()
{
mDirection = mtheDirection[mCurrentPos]; //Set the direction to what it should be
setDirection();
if(mDistance > mtheDistance[mCurrentPos]) //If the object has walked the full distance we increment the
//position in the array
{
if(mCurrentPos < mtheDirection.size()-1) //size of the array
{
mCurrentPos++;
}else{
mCurrentPos=0;
}
mDirection = mtheDirection[mCurrentPos];
mDistance = 0;
}else{ //Else we move the object in the correct direction
if(strcmp(mDirection, "N") == 0)
{
mY--;
}else if(strcmp(mDirection, "S") == 0)
{
mY++;
}else if(strcmp(mDirection, "W") == 0)
{
mX--;
}else if(strcmp(mDirection, "E") == 0)
{
mX++;
}
mDistance++; //Increment the distance the object has moved
}
}