appending parenthesis to char array with a defined format

P

puzzlecracker

Here is what I have thus far,

class Cell{
public:
const char * GetFormat();

};

char * Formatter (Cell *cell, double value)
{
char formatted[40];
if(value>=0)
sprintf(formatted, cell->GetFormat(), value);
else
// need to figure this out -- the explanation is below.
}

In the formatter function, I need to surround the resulting string
(character array) with parenthesis if the number is negative. What is
the most efficient way to do that? cannot change Cell class nor the
function's interface, and each cell has a different formatting. It's
similar to excel function to display negative values in parenthesis.

Hence if you pass -40 into the Formatter, you should get (20) as a
resulting string.


Thanks
 
P

puzzlecracker

puzzlecracker said:
Here is what I have thus far,
class Cell{
 public:
      const char * GetFormat();

char * Formatter (Cell *cell, double value)
{
      char formatted[40];
      if(value>=0)
         sprintf(formatted, cell->GetFormat(), value);
     else
       // need to figure this out -- the explanation is  below.
}
In the formatter function, I need  to surround the  resulting string
(character array) with parenthesis if the number is negative. What is
the most efficient way to do that? cannot change Cell class nor the
function's interface, and each cell has a different formatting. It's
similar to excel function to display negative values in parenthesis.

Probably something like

    else {
       formatted[0] = '(';
       sprintf(formatted + 1, cell->GetFormat(), -value);
       strcat(formatted, ")");
    }

but beware of overflowing buffers.
Hence if you pass -40 into the Formatter, you should get (20) as a
resulting string.

So, it will also divide by 2? ;-)

V

Thanks a little twist to the problem, I would like the parenthesis to
start right before the number. In the implementation above, depending
on a format, it may contain have spaces. You can assume there is
enough room in the buffer.
 
T

Thomas J. Gritzan

puzzlecracker said:
puzzlecracker said:
Here is what I have thus far,
class Cell{
public:
const char * GetFormat();
};
char * Formatter (Cell *cell, double value)
{
char formatted[40];
if(value>=0)
sprintf(formatted, cell->GetFormat(), value);
else
// need to figure this out -- the explanation is below.
}

Huh? You don't return any value, but the return type of the function
suggests that you want to return 'formatted'.
Don't return a pointer to a lokal array.
In the formatter function, I need to surround the resulting string
(character array) with parenthesis if the number is negative. What is
the most efficient way to do that? cannot change Cell class nor the
function's interface, and each cell has a different formatting. It's
similar to excel function to display negative values in parenthesis.
Probably something like

else {
formatted[0] = '(';
sprintf(formatted + 1, cell->GetFormat(), -value);
strcat(formatted, ")");
}

but beware of overflowing buffers.
Hence if you pass -40 into the Formatter, you should get (20) as a
resulting string.
So, it will also divide by 2? ;-)

V

Aren't you long enough in this group not to quote signatures anymore?
Thanks a little twist to the problem, I would like the parenthesis to
start right before the number. In the implementation above, depending
on a format, it may contain have spaces. You can assume there is
enough room in the buffer.

If there can be any number of whitespaces before and after the number,
you would need to loop through the string and move parts of it to make
space for the parenthesis.

I think you would get a better answer in comp.lang.c, since you don't
want a C++ solution, do you?
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,768
Messages
2,569,574
Members
45,051
Latest member
CarleyMcCr

Latest Threads

Top