• Advertisement
Sign in to follow this  

Output variable values

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

I have a quick question, i'm using the extension lib sdl_ttf to input the player1 and player2 score. The variables are global and defined right below the includes so they're acessable over the whole program. The score system is working fine but the output of the score is not going so well. When i'm using: sprintf(score, "Player 1: %0.f", player1Score); p2score = TTF_RenderText_Solid(font, score, fontColor); It somehow picks up that the player1Score is an int but not the value itself. Is there any other way to store the value in a variable than using sprintf if you're going to use both a string and a int in the same string?

Share this post


Link to post
Share on other sites
Advertisement

sprintf(score, "Player 1: %d", player1Score);
p2score = TTF_RenderText_Solid(font, score, fontColor);

%f is used for floats. %d for integers...

Share this post


Link to post
Share on other sites
I see, I didn't know that, I thought it was some kind of temporary holder for the variable, thank you.

Share this post


Link to post
Share on other sites
Quote:
Original post by password
I see, I didn't know that, I thought it was some kind of temporary holder for the variable, thank you.


It tells sprintf where to embed the value in the string.

BTW, if you are using c++ I highly recommend you use std::string for text and std::stringstream for this sort of conversion.


#include <string>
#include <sstream>

std::string score = "Player 1: ";
std::stringstream scorestream;
scorestream << player1Score;
score += scorestream.str();

p2score = TTF_RenderText_Solid(font, score.c_str() , fontColor);

Share this post


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

  • Advertisement