-
itoa()
Hello-
I'm trying to figure out how to render the position of my mouse on-screen.
Is there anyway to convert an integer to a string? I don't want to use char[].
Code:
static std::string mousex, mousey, text;
itoa(mouse.GetX(), mousex, 10);
itoa(mouse.GetY(), mousey, 10);
strcat(text, "X: ");
strcat(text, mousex);
strcat(text, " Y: ");
strcat(text, mousey);
TextOut(hDC, 50, 50, text, strlen(text));
-
It says C++ at the top of the forum, so I will suggest using a stringstream.
Code:
std::stringstream message;
message << "(" << x << ", " << y << ")";
TextOut(hDC, 50, 50, message.str().c_str(), message.str().length());
Not compiled because I'm not quite that bored, but should be close.
-
It works out great, thanks!
I have another problem though. All of this code is inside a timer event function, so it all repeats. The string now looks like: X: 400 Y: 300X: 400 Y: 300X: 400 Y: 300X: 400 Y: 300X: 400 Y: 300X: 400 Y: 300X: 400 Y: 300.... and so on. Is there any way to clear the string? I've tried text.str().clear() but it doesn't seem to work.
Any help is appreciated!
EDIT:
Never mind! I googled the problem and found the solution. I used .str("")