• Advertisement
Sign in to follow this  

sprintf %% escape sequence

This topic is 3315 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hey guys, I'm having issues using the literal text "%". Say I want to type a creature's health in percent, ie "100%". Shouldnt using the escape sequence "%%" create a literal percentage sign? Here's my code:
sprintf_s( strHealth, sizeof(strHealth), "%d %%", CreatureHealth);

I get a pop-up box runtime error saying
     Debug Assertion Failed!
     Program: c:\Users\Matt\Desktop\Game\Debug\Game.exe
     File: f:\dd\vctools\crt_bld\self_x86\crt\src\output.c
     Line: 2293

     Expression: ((state == ST_NORMAL) || (state == ST_TYPE))
I get assertion errors for a lot of (usually trivial) things and so finding data relevant to this specific instance has proved a tad challenging for me, especially as google/gamedev searches ignore percentage signs! Any help is greatly appreciated!

Share this post


Link to post
Share on other sites
Advertisement
Yes, %% should create a literal percentage sign. Two things though: First, ensure that strHealth is pointing to valid contiguous bit of memory. Second, remember that sizeof only works on ARRAYS. NOT pointers.

/* this works: */
char strHealth[10];
sizeof(strHealth);

/* this doesn't work: */
char * strHealth = /* whatever */
sizeof(strHealth); /* BAD */

/* this doesn't work either */
void function( char strHealth[] )
{ sizeof(strHealth); /* BAD */ }

Share this post


Link to post
Share on other sites
nobodynews: Thankyou, I am not using pointers, but I appreciate your feedback.

I have tested different code to verify where the issue lies and also to attempt to clarify the error somewhat:
/* sizeof() and the input integer have been replaced with hard coded versions to show they are not the issue*/

/* This code works perfectly (outputs '10')*/
char creaturehealth[20];
sprintf_s( creaturehealth, 20, "%d", 10);

/* Addition of %% gives the error */
char creaturehealth[20];
sprintf_s( creaturehealth, 20, "%d %%", 10);



Omitting the '%' isn't such a horrible solution.. but its inclusion seems trivial.

Share this post


Link to post
Share on other sites
Does the real sprintf work? It could be a bug in Microsoft's "safe" implementation. I use %% all the time and have never had problems.

Share this post


Link to post
Share on other sites
What version of Visual Studio are you using? The following works perfectly for me in Visual Studio 2008 SP1:


#include <stdio.h>
#include <iostream>

int main(int argc, char* argv[])
{
char buffer[20];

sprintf_s(buffer, "health: %d", 10);
std::cout << buffer << std::endl;

sprintf_s(buffer, "health: %d%%", 10);
std::cout << buffer << std::endl;

return 0;
}



Output is:


health: 10
health: 10%


Can you run my test program and tell me what happens? Perhaps the problem is somewhere else?

Share this post


Link to post
Share on other sites
Thankyou gekko and Codeka for the replies.

gekko: sprintf produces the same error, so no luck there.

Codeka: I am using Visual C++ 2008 Express Edition. Your test program ran perfectly, the output is identical to yours. How could this specific project mess with sprintf's proper execution? I'm afraid I'm confused!

After some more testing and googling, you also get this error if you don't escape the % sign. It's as if it is interpreting my "%%" as two separate "%".

Thanks,

Share this post


Link to post
Share on other sites
It's rather strange that one works and other does not. I would suggest the next step would be to run the program in a debugger, and when it hits that assertion, jump in and have a look at what it's doing. For example, you can look at the parameter passed in for the format to make sure it's what you expect, see what the state variable actually is (that is, the assertion is checking whether state is ST_NORMAL or ST_TYPE -- obviously, it's something else, what it is)? And so on...

But clearly, it is possible to get it working.

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement