Output variable values

Started by
2 comments, last by rip-off 18 years ago
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?
Advertisement

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

%f is used for floats. %d for integers...
I see, I didn't know that, I thought it was some kind of temporary holder for the variable, thank you.
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);

This topic is closed to new replies.

Advertisement