typecasting problem C/C++ ...

Started by
4 comments, last by HolyFish 22 years, 2 months ago
I have this variable : int Score=0; Now I want to increase the score with ,for example, 100 : Score += 100; But when I want to write that score to the screen (in DirectDraw) i have to use the TextOut() function, but it can only put strings on the screen, so I do typecasting: TextOut(Dc, 0, 0, (char*)Score, 10); ==> But then I get the character on the screen, who has the ASCII value of 100 !!! How can I just put the real Score value("100") on the screen ?? Thanks
Advertisement
A type cast definately won''t work. You have to use a function (like sprintf or wsprintf) to write the number to a string, then use TextOut to draw that string.
Ya:

char temp[50];
sprintf(temp, "%d", Score);
TextOut(Dc, 0, 0, temp, 10);

Of course, you probably shouldn't be using TextOut in DirectDraw except for early debugging maybe.

Abs

Edited by - Absolution on February 1, 2002 12:23:23 PM
You''ll need to convert the int to an array of char. Like Impossible pointed out, you''ll probably want to use sprintf (or wsprintf if you''re using UNICODE).

Here''s how you do it
  int score;char msg[80];sprintf(msg, "%i", score);TextOut(Dc, 0, 0, msg, 10);  
==========================================In a team, you either lead, follow or GET OUT OF THE WAY.
Allright, thanks all for the quick responses !!
If you are using C++ (which I assume you are judging by the topic) then you can use the lexical_cast in boost which allows you to cast standard types (and even non standard if they have written an << extraction function) to strings.


- Houdini

Edited by - Houdini on February 1, 2002 2:41:47 PM
- Houdini

This topic is closed to new replies.

Advertisement