Jump to content
  • Advertisement

Archived

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

HolyFish

typecasting problem C/C++ ...

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

If you intended to correct an error in the post then please contact us.

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 this post


Link to post
Share on other sites
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.

Share this post


Link to post
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 this post


Link to post
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 this post


Link to post
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

Share this post


Link to post
Share on other sites

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!