Allegro.cc - Online Community

Allegro.cc Forums » Programming Questions » How do I get a percent sign in a sprintf string?

Credits go to BAF, CGamesPlay, Jonatan Hedborg, miran, and X-G for helping out!
This thread is locked; no one can reply to it. rss feed Print
How do I get a percent sign in a sprintf string?
HardTranceFan
Member #7,317
June 2006
avatar

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?

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

miran
Member #2,407
June 2002

Did you try this:

char someStr[20];
sprintf(someStr, "%i\%", 25);

?

--
sig used to be here

Jonatan Hedborg
Member #4,886
July 2004
avatar

Odd that it doesn't work, "%%" should be printed out as "%". What is your compiler?

X-G
Member #856
December 2000
avatar

%% is the right format specifier. You (or your libc) must be doing something else wrong. More code.

--
Since 2008-Jun-18, democracy in Sweden is dead. | 悪霊退散!悪霊退散!怨霊、物の怪、困った時は ドーマン!セーマン!ドーマン!セーマン! 直ぐに呼びましょう陰陽師レッツゴー!

miran
Member #2,407
June 2002

Of course it prints '25'. You're probably doing it like this:

printf(someStr);

right? Or something along those lines.

Think about it. The code you use to print the string to the output or whatever also processes the % signs. You should do it like this:

printf("%s", someStr);

--
sig used to be here

CGamesPlay
Member #2,559
July 2002
avatar

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

--
Tomasu: Every time you read this: hugging!

Ryan Patterson - <http://cgamesplay.com/>

HardTranceFan
Member #7,317
June 2006
avatar

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.

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

BAF
Member #2,981
December 2002
avatar

Why sprintf then use textout? You do know there is a textprintf_ex for exactly this reason, right?

HardTranceFan
Member #7,317
June 2006
avatar

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]

--
"Shame your mind don't shine like your possessions do" - Faithless (I want more part 1)

BAF
Member #2,981
December 2002
avatar

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.

Go to: