• ### Popular Now

• 11
• 9
• 10
• 9
• 10

#### Archived

This topic is now archived and is closed to further replies.

# typecasting problem C/C++ ...

This topic is 5924 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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

##### Share on other sites
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.

##### Share on other sites
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

##### Share on other sites
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);

##### Share on other sites
Allright, thanks all for the quick responses !!

##### Share on other sites
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