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?
Odd that it doesn't work, "%%" should be printed out as "%". What is your compiler?
%% is the right format specifier. You (or your libc) must be doing something else wrong. More code.
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
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.
Why sprintf then use textout? You do know there is a textprintf_ex for exactly this reason, right?
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]
Thanks guys, Miran and BAF in particular - your comments sent me on a different train of thought. It ends up one of the methods was using textprintf_ex(...). I changed this to textout_ex(...), and it worked.
The result is a finished project/game.
[/edit]
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.