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.:
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.)
? ??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.:
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.)
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?
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.
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?