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, 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 ➜ Programming ➜ General ➜ problem with number formatting code.

problem with number formatting code.

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


Posted by Typhon   USA  (112 posts)  Bio
Date Sun 05 Oct 2008 04:23 PM (UTC)
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
Top

Posted by Zeno   USA  (2,871 posts)  Bio
Date Reply #1 on Sun 05 Oct 2008 05:33 PM (UTC)
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
Top

Posted by Typhon   USA  (112 posts)  Bio
Date Reply #2 on Sun 05 Oct 2008 06:06 PM (UTC)
Message
haha breaking up the code works. thanks :)
Top

Posted by Nick Cash   USA  (626 posts)  Bio
Date Reply #3 on Mon 06 Oct 2008 05:58 AM (UTC)

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
Top

Posted by Nick Gammon   Australia  (23,070 posts)  Bio   Forum Administrator
Date Reply #4 on Mon 06 Oct 2008 07:22 AM (UTC)
Message
You would be 100% correct about that.

- Nick Gammon

www.gammon.com.au, www.mushclient.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.


17,356 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.