13 Mar, 2010, triskaledia wrote in the 1st comment:
Votes: 0
Code:
if(wnum == 0)
aname = (char) 'ring'; //Works fine, no warnings.
else if(wnum == 1)
{
if(number_chance(50))
aname = 'talisman'; //warning: overflow in implicit constant conversion
else
aname = "amulet"; //warning: assignment makes integer from pointer without a cast
}
else if (wnum == 2)
aname = "breastplate";
else if (wnum == 3)
{
if(number_chance(33))
aname = "tiara";
else if(number_chance(34))
aname = "crown";
else
aname = "helmet";
}

Question:
Where RING is defined, does setting it as a (char) change it to a variable
that can be manipulated other places within the code?
…..
While typing this I realized I forgot to define char aname with a string
length, but still, the question above.^^^
13 Mar, 2010, David Haley wrote in the 2nd comment:
Votes: 0
(char) 'ring' isn't doing at all what you think it's doing. Double quotes mean strings, single quotes mean characters.

You can't assign a string into a char buf[xyz]. You need to use strcat. E.g., strcat(aname, "crown").
13 Mar, 2010, triskaledia wrote in the 3rd comment:
Votes: 0
Blah, guess I should keep testing around before I post…
Got an error now when I defined aname as
char aname[MSL];
error: incompatible types in assignment hits everywhere I have an object name setup.

And before someone asks, yes, I'm reinventing the random object wheel.
13 Mar, 2010, triskaledia wrote in the 4th comment:
Votes: 0
I guess I should also check for replies before I post again… haha.
Anyway, thanks David, I'll try that.
13 Mar, 2010, triskaledia wrote in the 5th comment:
Votes: 0
Another quick question:
What does strcat mean exactly, and it does it clear itself after the code block has been called and finished?
13 Mar, 2010, David Haley wrote in the 6th comment:
Votes: 0
strcat means to concatenate a string into a buffer. (I should add that for this to work, the target buffer must be correctly initialized to contain a zero byte as the first character.)

When you ask if "it" clears itself, what are you referring to? strcat just writes to a buffer. If that buffer was allocated on the stack (like char buf[123]) the buffer is removed when the scope exits, yes.
13 Mar, 2010, triskaledia wrote in the 7th comment:
Votes: 0
Thanks for the help on the strcat.
Now I am running into the problem with my variable aname not clearing itself out.
First kill I get: You grab a ring from the corpse of Bob. <– not actually the display but you'll see…
Second kill I get: You grab a ringbracer from the corpse of Bob.
Third kill I get: You grab a ringbracerring from the corpse of Bob.
etc etc etc.

I tried doing free_string(aname); to clear it out, but it started throwing some garbage into the display, which Im assuming is happening because Im clearing something that hasn't been set. Anyway, this is what I got for code.

void armor_load (CHAR_DATA * ch)
{
OBJ_DATA *armor;
char buf[512];

int anum = number_range(armor_num[0], armor_num[10]);
int wnum = number_range(0, 11);
int ldnum = number_range (0, 2);
char aname[MSL];

armor = create_object (get_obj_index (OBJ_VNUM_RARMOR), 0);

// free_string(aname);
if(wnum == 0)
strcat(aname, "ring");
else if(wnum == 1)
{
if(number_chance(50))
strcat(aname, "talisman");
else
strcat(aname, "amulet");
}
else if (wnum == 2)
strcat(aname, "breastplate");
else if (wnum == 3)
{
if(number_chance(15))
strcat(aname, "tiara");
else if(number_chance(30))
strcat(aname, "crown");
else
strcat(aname, "helmet");
}
else if (wnum == 4)
strcat(aname, "leggings");
else if (wnum == 5)
strcat(aname, "boots");
else if (wnum == 6)
strcat(aname, "gauntlets");
else if (wnum == 7)
strcat(aname,"sleeves");
else if (wnum == 8)
strcat(aname, "shield");
else if (wnum == 9)
strcat(aname, "cloak");
else if (wnum == 10)
strcat(aname, "belt");
else
strcat(aname, "bracer");

sprintf (buf, "%s %s", armor_name[anum], aname);
free_string (armor->name);
armor->name = str_dup (buf);
sprintf (buf, "%s %s", armor_short[anum], aname);
free_string (armor->short_descr);
armor->short_descr = str_dup (buf);

sprintf (buf, "%s %s %s", armor_long[anum], aname, armor_long2[ldnum]);
free_string (armor->description);
armor->description = str_dup (buf);

armor->timer = -1;
armor->cost = number_range(100, 1000);
armor->weight = number_range(1, 3);
armor->level = number_range(ch->level - 5, ch->level + 5);

armor->value[0] = number_range((ch->level / 3) - 5, (ch->level / 3) + 5);
armor->value[1] = number_range((ch->level / 3) - 5, (ch->level / 3) + 5);
armor->value[2] = number_range((ch->level / 3) - 5, (ch->level / 3) + 5);
armor->value[3] = number_range((ch->level / 3) - 5, (ch->level / 3) + 5);

obj_to_char (armor, ch);
sprintf(buf, "You grab %s from the corpse.\n\r", armor->short_descr);
stc(buf, ch);

return;
}
13 Mar, 2010, Davion wrote in the 8th comment:
Votes: 0
strcat concatinates. It does not set the string. Meaning, whatever is in there when you call strcat, will be there after strcat. free_string() and str_dup() are both functions that are involved in allocating dynamic strings. You're using automatics (denoted by the [MSL] part). You'll want to use sprintf() (or better yet, snprintf() ) to set the value you want in aname.
13 Mar, 2010, triskaledia wrote in the 9th comment:
Votes: 0
Thanks for the help. I got that error taken care of now.
13 Mar, 2010, David Haley wrote in the 10th comment:
Votes: 0
Yes, like I said if you use strcat, you need to set the buffer's first byte to a zero before using strcat.
13 Mar, 2010, Sharmair wrote in the 11th comment:
Votes: 0
The strcat function tacks your string to the end of the target buffer that is assumed
to already contain a valid string. Setting the first char to zero is just setting the buffer
to an empty string to start with. I would use strcpy instead as it ignores the contents
of the buffer to start and just copies your string to the buffer (the syntax is the same
as strcat).
Well, actually, I would not use either strcat or strcpy in this case. I would make aname
a const char* and just assign it to your literal strings (sort of like you seemed to have
started doing, but had aname declared wrong).
Also, as a side note, the [MSL] or whatever has nothing at all to do with something
being auto, dynamic or static (though the comment about not using the dynamic mem
functions on autos is correct).
0.0/11