|
problem with number formatting code.
|
Reply to this subject
Start a new subject
 
Refresh page
| Posted by |
Typhon
USA (112 posts) 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 | top |
|
| Posted by |
Zeno
USA (2,867 posts) bio
Moderator |
| Date |
Reply #1 on 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 | top |
|
| Posted by |
Typhon
USA (112 posts) bio
|
| Date |
Reply #2 on Sun 05 Oct 2008 06:06 PM (UTC) [ quote
] |
| 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) [ quote
] Amended on Mon 06 Oct 2008 06:01 AM (UTC) by Nick Cash
|
| Message |
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 (18,772 posts) bio
Forum Administrator |
| Date |
Reply #4 on 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 | 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,439 views.
Reply to this subject
Start a new subject
 
Refresh page
top
Comments to:
Gammon Software support
Forum RSS feed ( http://www.gammon.com.au/rss/forum.xml )