Best option I think is to pass fnum a separate buffer for each call.

char buffer1[16], buffer2[16];
...
printf_to_ch(ch, "Your current balance is: %s Steel and %s Gold.\n\r",
    fnum(1000, buffer1), fnum(2000, buffer2));

Modifying fnum should be simple then, just change buf_new to an argument:

char *fnum(long number, char *buf_new)
{
   int index,index_new,rest;
   char buf[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;
}

--Palrich.

----- Original Message ----- 
From: "Davion Kalhen" <[EMAIL PROTECTED]>
To: <[email protected]>
Sent: Wednesday, February 11, 2004 11:39 PM
Subject: Re: Formatting numbers


> Its not really a problem you can fix, persay. As in, getting it to work your 
> way would be... uhh interesting? But, I was pondering over this and so I 
> talked to one of my Guru's to get an explination. And, now, with sprintf it 
> does the last in first out thing, right? So the second one is process first, 
> then the first. They are both pointing to the same static buffer, which is 
> why you get the resaults you do. Not much you can do with this, except, 
> maybe have it return strdup(new_buf); but then you'd have to free it to save 
> a massive memory leak. Eg.
> 
> char *tmp, *tmp2;
> 
> tmp = fnum(ch->steal);
> tmp2 = fnum(ch->gold);
> sprintf(buf, "%s %s\n\r", tmp, tmp2);
> 
> Now, that doesn't really save time, or lines. You can also (Without mods)
> 
> int len;
> 
> len = sprintf(buf, "%s", fnum(ch->steal) );
> sprintf(buf+len, "%s", fnum(ch->gold) );
> 
> Thats alittle better :), but, this is all I can throw up right now. Sorry if 
> the function names are alittle off, but I deleted your post acsodently and 
> can't find recient archives.
> 
> Davion


Reply via email to