I have a function that I got from a snippet that I need a little help with.
It seems to work fine the first time it is called but not the second time.
Here is the code I was using to test it with:
printf_to_ch(ch, "Your current balance is: %s Steel and %s Gold.\n\r",
fnum(1000), fnum(2000));
What this outputs is:
Your current balance is: 1,000 Steel and 1,000 Gold.
When it should be:
Your current balance is: 1,000 Steel and 2,000 Gold.
Here is the fnum function in question:
char *fnum(long number)
{
int index,index_new,rest;
char buf[16];
static char buf_new[16];
sprintf(buf, "%ld", number);
rest = strlen(buf)%3;
for (index = index_new = 0;index < strlen(buf); index++, index_new++)
{
if (index != 0 && (index - rest) %3 == 0 )
{
buf_new[index_new]=',';
index_new++;
buf_new[index_new] = buf[index];
}
else
buf_new[index_new] = buf[index];
}
buf_new[index_new] = '\0';
return buf_new;
}
Any help on this would be very much appreciated.