You can use a static multi-buffer output.
new function would be like:

char *fnum(long number)
{
     int index,index_new,rest;
     char buf[16];
     static char buf_new[5][16];        // allows for 5 consecutive
calls
     static int I;                      // increment counter
     char *result;

     sprintf(buf, "%ld", number);
     rest = strlen(buf)%3;

     ++I, I %= 5; // increment the buffer
     result = buf_new[I];

     for (index = index_new = 0;index < strlen(buf); index++,
index_new++)
     {
          if (index != 0 && (index - rest) %3 == 0 )
          {
                   result[index_new]=',';
               index_new++;
                   result[index_new] = buf[index];
          }
          else
                   result[index_new] = buf[index];
     }
     result[index_new] = '\0';
     return result;
}

You could also make it more fun by passing a CHAR_DATA pointer and
making it plr configurable.

Markanth <[EMAIL PROTECTED]>

> 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.


Reply via email to