• Advertisement
Sign in to follow this  

displaying second in the right format

This topic is 2939 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

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 :)

Share this post


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

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

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.

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.

Share this post


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

  • Advertisement