[C++/SDL] How to render integers with SDL_TTF

Started by
5 comments, last by swiftcoder 11 years, 3 months ago

Hello i am currently creating my first SDL game and i need a little counter on the side of my screen that displays the score however when i do the normal


   message = TTF_RenderText_Solid( font,score textColor );

i get an error stating i cannot convert a int to char*,How can i overcome this?

Advertisement

You might not have provided enough information for someone to help. But I'll take a stab. Also I think you have a typo and missed a comma in the code you did post.

I am assuming 'score' is an int and the function, TTF_RenderText_Solid is expecting a char* for the second arguement.

A simple way to do this would be:


char textBuffer[64];

sprintf(textBuffer, "%d", score);  // Write the int 'score' into a char buffer

TTF_RenderText_Solid(font, textBuffer, textColor);


sprintf works just like printf but writes into a char buffer instead of standard out.

The error you received was pretty straight forward. You were trying to use a variable of type 'int' when a variable of type 'char*' was expected.

The OP tagged the topic as C++, so supplying the native C++ way to do this would have been better (especially since it's clear the OP is still lacking a lot of experience and the *printf-family of C function can get you quickly into difficult to find, obscure bugs if you are careless):


#include <sstream>

...

std::ostringstream oss;
oss << score;
TTF_RenderText_Solid(font, oss.str().c_str(), textColor);

The OP tagged the topic as C++, so supplying the native C++ way to do this would have been better (especially since it's clear the OP is still lacking a lot of experience and the *printf-family of C function can get you quickly into difficult to find, obscure bugs if you are careless):



#include <sstream>

...

std::ostringstream oss;
oss << score;
TTF_RenderText_Solid(font, oss.str().c_str(), textColor);

Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?

Answer to question:

To convert "int" to "char" use stringstream.

First include it

#include <stringstream>

Second declare a object of stringstream

std::stringstream objSS;

Third store data intro stringstream

int my_int = 100; char my_char = 'a'; std::string my_string = "Good day sir"; objSS << my_int; objSS << my_char; objSS << my_string; std::cout << objSS.str() << std::endl; 100aGood day sir To clear already used stringstream objSS.clear();//This will clear any errors that may occured objSS.str(std::string()); //This will clear your stringstream object allowing for reuse

<blockquote class="ipsBlockquote" data-author="BaneTrapper" data-cid="5022847"><p>Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?</p></blockquote><br /><a data-cke-saved-href="http://en.cppreference.com/w/cpp/io/basic_ostringstream" href="http://en.cppreference.com/w/cpp/io/basic_ostringstream">Linky</a>
<blockquote class="ipsBlockquote" data-author="BaneTrapper" data-cid="5022847"><p>Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?</p></blockquote><br /><a data-cke-saved-href="http://en.cppreference.com/w/cpp/io/basic_ostringstream" href="http://en.cppreference.com/w/cpp/io/basic_ostringstream">Linky</a>

Thank you for the webpage.

[quote name='BaneTrapper' timestamp='1358505267' post='5022847']
Google does not provide info on "ostringstream" is that same or equal to "stringstream" ?[/quote]

All standard library stream classes come in 'input' and 'output' varieties, prefixed respectively with 'i' and 'o'.

The non-prefix version is a bidirectional stream which may be used to interleave both input and output on the same underlying stream object.

Tristam MacDonald. Ex-BigTech Software Engineer. Future farmer. [https://trist.am]

This topic is closed to new replies.

Advertisement