itoa()

This is a discussion on itoa() within the C++ Programming forums, part of the General Programming Boards category; Hello- I'm trying to figure out how to render the position of my mouse on-screen. Is there anyway to convert ...

  1. #1
    Registered User
    Join Date
    Apr 2009
    Posts
    45

    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));

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    Registered User
    Join Date
    Apr 2009
    Posts
    45
    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("")
    Last edited by Flaug; 09-23-2009 at 06:10 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem using itoa
    By g_p in forum C Programming
    Replies: 2
    Last Post: 05-03-2008, 06:38 AM
  2. Problem with itoa() perhaps?
    By TheSquid in forum C++ Programming
    Replies: 5
    Last Post: 05-08-2006, 02:04 AM
  3. Really Weird itoa Problem
    By Grantyt3 in forum C++ Programming
    Replies: 8
    Last Post: 12-19-2005, 11:44 PM
  4. itoa
    By coldcoyote in forum Linux Programming
    Replies: 4
    Last Post: 02-13-2003, 08:28 AM
  5. itoa
    By Thantos in forum C Programming
    Replies: 2
    Last Post: 09-18-2001, 02:23 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21