[Home] [Downloads] [Search] [Help/forum]

Gammon Software Solutions forum

See www.mushclient.com/spam for dealing with forum spam. Please read the MUSHclient FAQ!

[Folder]  Entire forum
-> [Folder]  Programming
. -> [Folder]  General
. . -> [Subject]  problem with number formatting code.

Home  |  Users  |  Search  |  FAQ
Username:
Register forum user name
Password:
Forgotten password?
(New message)
Subject: problem with number formatting code.
Name:
Your forum user name.
Register forum user name
Password:
Your forum password.
Forgotten password?
Message:
Message to be posted (in English, please).
Forum codes:
Check this if your message uses 'forum codes' or templates (auto-detected for new posts).
Forum codes Templates

Save this message ...


Subject review (reverse sequence)

Posted by Nick Gammon   Australia  (18,800 posts)  [Biography] bio   Forum Administrator
Date Mon 06 Oct 2008 07:22 AM (UTC)  quote  ]
Message
You would be 100% correct about that.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Mon 06 Oct 2008 05:58 AM (UTC)  quote  ]

Amended on Mon 06 Oct 2008 06:01 AM (UTC) by Nick Cash

Message
static char buf[12];

Although you figured it out, I'd like to point out it is the use of a static variable that causes this. It avoids memory allocation management, but at the expense of situations like this. As you have discovered, it works if you break up the code calling the functions.

If I had to guess, the value retained in the buffer is from the last call completed since the return value each time points to the same static character array. Because you put two calls in one line, the second call was stacked on the first call, thus executing the second call first, causing it's value in 'buf' to be wiped out by the next call.

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Typhon   USA  (112 posts)  [Biography] bio
Date Sun 05 Oct 2008 06:06 PM (UTC)  quote  ]
Message
haha breaking up the code works. thanks :)
[Go to top] top

Posted by Zeno   USA  (2,868 posts)  [Biography] bio   Moderator
Date Sun 05 Oct 2008 05:33 PM (UTC)  quote  ]
Message
This may be related, but in Smaug if you use num_punct (which does the same thing) on the same line, it won't work.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Typhon   USA  (112 posts)  [Biography] bio
Date Sun 05 Oct 2008 04:23 PM (UTC)  quote  ]
Message
seems like i'm missing something real small here but nonetheless i cant find it.
added :

char *short_num(int num)
{
	static char buf[12];
	buf[0] = '\0';
	
	if (num <= 9999)
	{
		sprintf(buf, "%d", num);
		return buf;
	}
	sprintf(buf, "%5.1f%c", (float)num / 1000, num>1000000 ? 'm' : 'k' );
	return buf;
}


when calling that function in
ch_printf(ch, "%-4s/%-4s ", short_num(ch->hit), short_num(ch->max_hit) ); 


it returns the exact same figure for both :(
11.4k/11.4k when ch has 30,000 max_hit.
thanks
-typ
[Go to top] 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.


2,464 views.

[Reply to this subject]  Reply to this subject   [New subject]  Start a new subject   [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

[Home]

Written by Nick Gammon - 5K

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Internet Contents Rating Association (ICRA) - 2K]    [Web site powered by FutureQuest.Net]