Output variable values

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 on other sites

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

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

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 on other sites
Quote:
 Original post by passwordI 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);

Create an account

Register a new account

• Forum Statistics

• Total Topics
627752
• Total Posts
2978920

• 10
• 10
• 22
• 14
• 14