Sign in to follow this  
Followers 0
ianmclean0001

displaying second in the right format

6 posts in this topic

Hi there I have being working on my first game. it is really simple but I am have trouble display the seconds in the correct way ie if DISPLAY_SECOND <= 9 print "0" + DISPLAY_SECOND else print DISPLAY_SECOND
    char Buffer[255]; 
    sprintf(Buffer, "Time Taken: %d:%d ",CURRENT_SECONDS/60,DISPLAY_SECONDS);
    text.drawText(game->screen, Buffer, small_font, 255, 255, 255, 5, 2);
but I am unsure on how to manipulate the string thanks for your time hope you can help ian :)
0

Share this post


Link to post
Share on other sites
int mins = current_seconds/60;
int secs = current_seconds - mins*60;
sprintf(buffer,"Time %d:%c%d",mins, secs < 10 ? '0':'''',secs);
0

Share this post


Link to post
Share on other sites
thanks buckeye I didn't think of using an inline if

but I'm getting an empty character constant

don;t think c++ like the :'' part of the if statement

secs < 10 ? '0':''

cheers

ian
0

Share this post


Link to post
Share on other sites
Ah. Use a string specifier.

sprintf(buffer,"Time %d:%s%d",mins, secs < 10 ? "0":"",secs);

EDIT: szecs' is better.
0

Share this post


Link to post
Share on other sites
I humbly propose this as an even better solution: ;)

char buffer[255];
int mins = current_seconds/60;
int secs = current_seconds%60;
snprintf(buffer, sizeof(buffer), "Time %02d:%02d", mins, secs);


The calculation of the seconds looks nicer and a potential buffer overflow was avoided. Still not good with negative values for current_seconds.
0

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!


Register a new account

Sign in

Already have an account? Sign in here.


Sign In Now
Sign in to follow this  
Followers 0