|
How do I get a percent sign in a sprintf string? |
HardTranceFan
Member #7,317
June 2006
|
I want to get a string to hold a percentage, complete with the '%' sign. However, if I try: char someStr[20]; sprintf(someStr, "%i%%", 25); someStr holds "25". I want it to be "25%". What do I need to do to get the % sign in the string? -- |
miran
Member #2,407
June 2002
|
-- |
Jonatan Hedborg
Member #4,886
July 2004
|
Odd that it doesn't work, "%%" should be printed out as "%". What is your compiler?
|
X-G
Member #856
December 2000
|
%% is the right format specifier. You (or your libc) must be doing something else wrong. More code. -- |
miran
Member #2,407
June 2002
|
CGamesPlay
Member #2,559
July 2002
|
Quote: You're probably doing it like this: (Not my problem, but) Ah, nice catch! This reminds me of the time I learned how to use printf: Printing a score -- Ryan Patterson - <http://cgamesplay.com/> |
HardTranceFan
Member #7,317
June 2006
|
I'm using textout_ex(...) to print the string. I'm at work at the mo, so can't provide more code. I'll try a bit more debugging tonight, and follow the string through to see where in the process it loses the percent sign. -- |
BAF
Member #2,981
December 2002
|
Why sprintf then use textout? You do know there is a textprintf_ex for exactly this reason, right? |
HardTranceFan
Member #7,317
June 2006
|
My textout_ex(...) is in a class specifically for printing text to the screen with particular effects, and it keeps track of the state the printing effect is at. The sprintf is used to generate the string to pass to one of the methods in that class, rather than program the text class to do the text generation. I'm keeping the class such that I can just dump the code into another program without having to add or rewrite any of it (e.g. code it to format strings a particular way). However, if it's simple to pass an arbitrary number of arguments to a function/method, then I'd be keen to hear about it (my C/C++ skills have a long way to go), particularly if it means I can avoid this percentage sign problem. [edit] The result is a finished project/game. -- |
BAF
Member #2,981
December 2002
|
You can use stdargs.h and the va_args stuff to pass an arbitrary amount. I believe you want to look for vsprintf to handle the printf formatting right in your class. |
|