Writing numbers/ints with TTF

Hello there, and good evening.
I an currently developing my first, tiny game and I’ve come across some problems.

So, to catch the problems, I would be happy to read the value of different stuff going on in the game.
Let us say, I want to know the x and y axis of the player. Both are integers.
I have tried to put the values on the screen with TTF, but then I get the error that I can’t put “int” to “char”.

Is there a way for me to write integers on the screen?

The C++ standard library provides at least a couple of ways to convert numerical values to string form.

If you want to go the C way, look into sprintf() and friends. (I haven’t used these functions in a while, so I’m not sure if that’s the exact one you want. You should be able to find what you’re looking for online though.)

If you want to go the C++ way, you can use string streams, e.g.:

Code:
#include
#include

// …

std::stringstream stream;
std::string text;
stream << value;
stream >> text;

You can then access the string contents in C-style null-terminated form using string::c_str().

There’s also boost::lexical_cast<>(), which basically does what the above code does, but wraps it up nicely and takes care of the details for you. (lexical_cast might also be included in newer versions of the standard library, but I can’t remember for sure.)

It would be easier to do

? ??std::ostringstream string;
string << value;
string.str(); // For an std::string
string.str().c_str(); // for a a c string.

boost::lexical_cast() is best for converting from a string (C or
standard) to type T. It is also being considered for the TR2
specification for C++0x, but is not currently included in any standard
library.

(Just to show this works, here is a complete example for C+)

#include
#include
#include

int main(void)
{
int value = 10;
std::ostringstream string;
string << value;
std::cout << "STD::STRING: " << string.str() << std::endl;
std::cout << "C STRING: " << string.str().c_str() << std::endl;
}On Sun, Aug 22, 2010 at 2:03 PM, Jesse A. wrote:

The C++ standard library provides at least a couple of ways to convert numerical values to string form.

If you want to go the C way, look into sprintf() and friends. (I haven’t used these functions in a while, so I’m not sure if that’s the exact one you want. You should be able to find what you’re looking for online though.)

If you want to go the C++ way, you can use string streams, e.g.:

Code:

#include
#include

// …

std::stringstream stream;
std::string text;
stream << value;
stream >> text;

You can then access the string contents in C-style null-terminated form using string::c_str().

There’s also boost::lexical_cast<>(), which basically does what the above code does, but wraps it up nicely and takes care of the details for you. (lexical_cast might also be included in newer versions of the standard library, but I can’t remember for sure.)

Tres Walsh wrote:

boost::lexical_cast() is best for converting from a string (C or
standard) to type T.

Do you mean that lexical_cast() is more suited for converting from a string to type T than for converting from type T to a string? If so, can you clarify why that is?

Jesse A. wrote:

Tres Walsh wrote:

boost::lexical_cast() is best for converting from a string (C or
standard) to type T.

Do you mean that lexical_cast() is more suited for converting from a string to type T than for converting from type T to a string? If so, can you clarify why that is?

Lexical cast is defined as: "The ability to cast strings to other data types, and non-string types to strings."
I’ve never used boost::lexical_cast, but looking at the documentation I see no reason for his case to be true.

char* int_str = boost::lexical_cast<char*>(my_int);

should work fine O_o------------------------
EM3 Nathaniel Fries, U.S. Navy

http://natefries.net/

char* int_str = boost::lexical_cast<char*>(my_int);

That actually won’t work (in order for it to work, lexical_cast() would have to allocate a buffer for you). But, this should work fine:

Code:
std::string int_str = boost::lexical_caststd::string(my_int);

Nechs wrote:

I used this method, and it worked perfect for my needs:

std::stringstream stream;
std::string text;
stream << value;
stream >> text;

As Tres Walsh pointed out, you can just use str() to acquire the string (I just wasn’t thinking when I wrote that example).

It’s basically equivalent, but might save you a line or two of code :slight_smile:

You can disregard that statement, I don’t know why I wrote it :POn Sun, Aug 22, 2010 at 4:34 PM, Jesse A. wrote:

Tres Walsh wrote:

boost::lexical_cast() is best for converting from a string (C or
standard) to type T.

Do you mean that lexical_cast() is more suited for converting from a string to type T than for converting from type T to a string? If so, can you clarify why that is?

I am so happy for the replies!
I got the numbers printed on the screen, and I also got to do some nice changes with my map editor.
Thanks a lot! (:

I used this method, and it worked perfect for my needs:

std::stringstream stream;
std::string text;
stream << value;
stream >> text;

Will try out the other methods soon, to see if it can get any simpler.