Originally Posted by
CornedBee
To build on what 7stud said, copying a string from a 35-char buffer to a 30-char buffer is just asking for trouble.
Actually I started with two 30's figured I was overrunning the space in one to cause that error.... might go look into the null char. Mighta thought I had it in another part thanks for the help. Gonna go look now.
edit:
worked a bit more put some outputs in so I can log what contains what info. Here is the new code more comments some other things were changed nothing big.
Code:
if(temps[0] == 'l' && temps[1] == 'o' && temps[2] == 'o' && temps[3] == 'k')
{
log<<"Look was triggered"<<endl;
log<<"temps = "<<temps<<endl;
int number = 0;
int fix = 0;
char searchstr[30],tempstr[35];//changed code further down this is safe now.
char lookmsg[100];
for(int y=0;y<30;y++)
{
if(y == 0 && temps[y+5] - '0' < 10) //if we are using a number to define the creature
{ //ex 2.Goblin
fix = 2; //need to move the chars 2 to the left.
}
if(temps[y+5] == ' ') //if we are done
{
searchstr[y-fix] = '\0'; //NULL char...
break; //kick out of the loop
}
else //if not next character in
{
searchstr[y-fix] = temps[y+5];
}
}
log<<"Search String = "<<searchstr<<endl;
strcpy(tempstr," \0"); //is this better than " "?
strcat(tempstr,searchstr); //so I should have " Goblin" or what have you.
log<<"tempstr = \""<<tempstr<<"\""<<endl;
if(fix == 2) //is it a number? Found this is faster to check
{
for(int x=0;x<LNPCS;x++) //loop through all the NPCS in the game
{
if(npcs[x].IsHere() == Players[ID].IsHere() && strcmp(npcs[x].GetName(),tempstr) == 0)
{//If the NPC is in the same spot as the player AND its name == what I sent.
number++;
log<<"Found a NPC with a matching name and is in correct spot."<<endl;
if(fix == 2 && number == (temps[5] -'0'))//ok weird... dunno what I was thinkin
{
npcs[x].MakeLookMsg(lookmsg);
cons[ID].send((unsigned char)strlen("CHAT"));
cons[ID].send("CHAT");
cons[ID].send((unsigned char)strlen(lookmsg));
cons[ID].send(lookmsg);
}
}
}
}
else
{
for(int x=0;x<LNPCS;x++)
{
if(npcs[x].IsHere() == Players[ID].IsHere() && strcmp(npcs[x].GetName(),tempstr) == 0)
{
npcs[x].MakeLookMsg(lookmsg);
cons[ID].send((unsigned char)strlen("CHAT"));
cons[ID].send("CHAT");
cons[ID].send((unsigned char)strlen(lookmsg));
cons[ID].send(lookmsg);
}
}
}
}else
This is the relevant log file.
Code:
COMMAND: look Goblin
Look was triggered
temps = look Goblin
Search String = Goblin
tempstr = " Goblin"
OK so I just now noticed the line it was giving me wasn't even in this whole if it is at the end of the function so does that mean it is having trouble freeing the memory or something?