displaying second in the right format

Started by
5 comments, last by kolrabi 14 years, 2 months ago
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 :)
Advertisement
int mins = current_seconds/60;
int secs = current_seconds - mins*60;
sprintf(buffer,"Time %d:%c%d",mins, secs < 10 ? '0':'''',secs);

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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
Your format string should be like this:
"Time Taken: %02d:%02d"
cheers szecs

that worked perfectly

thanks again guys

Ian :)
Ah. Use a string specifier.

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

EDIT: szecs' is better.

Please don't PM me with questions. Post them in the forums for everyone's benefit, and I can embarrass myself publicly.

You don't forget how to play when you grow old; you grow old when you forget how to play.

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

This topic is closed to new replies.

Advertisement