Thread: Using TextOut to display variables

  1. #1
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532

    Using TextOut to display variables

    I searched google for this a couple of different times,rephrasing my search query. I have an int variable and need to display it on a window. I know there is a way you can format the variable so you can pass it to the TextOut function. I was wondering if anybody knew how to format the variable so TextOut will accept it? I tried this code but it doesn't display it, it compiles though:
    Code:
    TextOut(hDC,x,y,(LPCTSTR)clicks,2);
    clicks is my int variable and x and y are the position. That code compiles but it doesn't show the variable on the window?
    A different way is acceptable as well. Thanks.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  2. #2
    Registered User
    Join Date
    Apr 2002
    Posts
    1,571
    Look up sprintf
    "...the results are undefined, and we all know what "undefined" means: it means it works during development, it works during testing, and it blows up in your most important customers' faces." --Scott Meyers

  3. #3
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    I have and tried it but always get these errors:

    passing `const TCHAR *' as argument 1 of `sprintf(char *, const char *, ...)' discards qualifiers

    passing `int' to argument 2 of `sprintf(char *, const char *, ...)' lacks a cast

    But I'm pretty sure the way I have seen it done before was using sprintf or some similar function. This is what I passed to sprintf: for the buffer to receive the formatted string I used a variable of type LPCTSTR, TextOut accepts that kind of variable, and the second argument of sprintf I passed my int variable that needs formatting. I just used NULL for the last argument. Thanks for the suggestion though.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

  4. #4
    It's full of stars adrianxw's Avatar
    Join Date
    Aug 2001
    Posts
    4,829
    sprintf() updates the contents of the first parameter, thus it cannot be declared as const. It expects a formatting string as the second argument, not an integer.

    char SomeBuffer[20];
    int SomeNumber = 5;
    sprintf(SomeBuffer, "%d", SomeNumber);
    Wave upon wave of demented avengers march cheerfully out of obscurity unto the dream.

  5. #5
    C++ Enthusiast jmd15's Avatar
    Join Date
    Mar 2005
    Location
    MI
    Posts
    532
    Thank you that answered my question and solved my problem.
    Trinity: "Neo... nobody has ever done this before."
    Neo: "That's why it's going to work."
    c9915ec6c1f3b876ddf38514adbb94f0

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Best way to avoid using global variables
    By Canadian0469 in forum C++ Programming
    Replies: 7
    Last Post: 12-18-2008, 12:02 PM
  2. Father and Son Variables
    By khdani in forum Linux Programming
    Replies: 3
    Last Post: 11-28-2008, 06:42 PM
  3. Replies: 15
    Last Post: 09-30-2008, 02:12 AM
  4. hwnd and variables in them
    By underthesun in forum Windows Programming
    Replies: 6
    Last Post: 01-16-2005, 06:39 PM
  5. display variables
    By threahdead in forum C Programming
    Replies: 2
    Last Post: 10-01-2002, 08:55 AM