Register forum user name Search FAQ

Gammon Forum

Notice: Any messages purporting to come from this site telling you that your password has expired, or that you need to verify your details, confirm your email, resolve issues, making threats, or asking for money, are spam. We do not email users with any such messages. If you have lost your password you can obtain a new one by using the password reset link.
 Entire forum ➜ SMAUG ➜ SMAUG coding ➜ Misc. characters popping up in character strings?

Misc. characters popping up in character strings?

It is now over 60 days since the last post. This thread is closed.     Refresh page


Posted by Dace K   Canada  (169 posts)  Bio
Date Tue 04 Apr 2006 05:31 PM (UTC)

Amended on Tue 04 Apr 2006 05:48 PM (UTC) by Dace K

Message
I have a little piece of code that creates random strings from the following:

char letters[] = "abcdefghijklmnopqrstuvwxyz1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZ!@#$%*(),.;";


with the following:


	buf[0] = '\0';

	max = 3 + (2 * (int)(mob->level / 10));

	for (a=0; a<max; a++)
	{

		temp=number_range(1,strlen(letters)-1);
		buf[a]=letters[temp];
	}
	buf[a+1]='\0';

For some reason, some strings end in untranslatable characters, like upside-down question marks and odd squares.
It shouldn't be left-over junk in the buf char, because I've kind of cleared it with the initial
buf[0] = '\0';


I'm starting to think it's calling some special ascii characters, but since all it's using is !@#$%*(),.;, I don't see why that's happening.

So.. any idea why this is happening?

Thanks :)

ASJ Games - .Dimension 2, Resident Evil, and snippets - oh my!
http://asj.mudmagic.com

Drop by the area archives and find something for your mud. http://areaarchives.servegame.com
Top

Posted by David Haley   USA  (3,881 posts)  Bio
Date Reply #1 on Tue 04 Apr 2006 07:53 PM (UTC)
Message
For starters, setting buf[0] to \0 doesn't clear the buffer at all, it just "clears" the string contained in the buffer starting at position 0 (recall the strict definition of a string in C).

Now, here's the thing. You set everything in the interval [a;max[ (that is: a included, max excluded).

When the for loop ends, a = max.

Then you set a+1 to \0.

So you have:
[a;max[ : valid data
max : ???
max+1 : terminating 0

There's your problem. :-)

There are two ways to solve this:
1. properly clear the buffer (silly)
2. set \0 on max, not max+1 (smart)

David Haley aka Ksilyan
Head Programmer,
Legends of the Darkstone

http://david.the-haleys.org
Top

Posted by Dace K   Canada  (169 posts)  Bio
Date Reply #2 on Wed 05 Apr 2006 12:13 AM (UTC)
Message
Tehehe.
Such a silly mistake - thanks for pointing it out man :)

ASJ Games - .Dimension 2, Resident Evil, and snippets - oh my!
http://asj.mudmagic.com

Drop by the area archives and find something for your mud. http://areaarchives.servegame.com
Top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


10,710 views.

It is now over 60 days since the last post. This thread is closed.     Refresh page

Go to topic:           Search the forum


[Go to top] top

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.