• Advertisement
Sign in to follow this  

Ridiculous DWORD error (SOLVED - thanks)

This topic is 3970 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

DWORD g_num = 0; g_num--; if (g_num<0) g_num=5; PRINT("gnum = ",g_num); OUTPUT!!!: gnum = -1 Visual Studio 2005 glitch? I can't understand why this doesn't work... Update: DWORD g_num = 0; g_num--; if (g_num<0) g_num=5; if (g_num==-1) g_num=5 PRINT("gnum = ",g_num); OUTPUT: gnum = 5 What the heck is going on here?????????? Last time I checked, -1 is less than 0..... [Edited by - renman29 on April 7, 2007 8:16:34 AM]

Share this post


Link to post
Share on other sites
Advertisement
Isn't DWORD unsigned? Aren't you using a printf-style function in PRINT to print its value, possibly with the %d directive, which is for signed integers?

Share this post


Link to post
Share on other sites
DWORD is typedef'd as an unsigned long. So 0 - 1 in unsigned evaluates to 4294967295. Which is certainly not less than 0, so the g_num=5; code is never executred.

Share this post


Link to post
Share on other sites
also you should try

if ((int)g_num<0)

The DWORD needs to be an int (or some other signed type)for proper conparison and it seems the compiler doesnt do the conversion for you.

Share this post


Link to post
Share on other sites
Instead of casting it, why not just use an int variable in the first place?

Share this post


Link to post
Share on other sites
A good idea - in this circumstance I need a DWORD var - so what I actually ended up doing was something like this:

if (g_num<direction_increment)
g_num=MY_MAX-(direction_increment-g_num)+direction_increment;
gnum+=direction_increment;
if (g_num>MY_MAX) g_num=(g_num-MY_MAX);



Which is basically this sort of anticipation-logic:
if (gnum==0) gnum=5+1;
gnum--; //gnum becomes 5

I can't believe I didn't realize DWORD's unsigned.. I guess what threw me off was the fact that my PRINT function was still saying -1.
I'll stick to int's as much as possible. Thanks. :)

Share this post


Link to post
Share on other sites
Quote:
Original post by renman29
I guess what threw me off was the fact that my PRINT function was still saying -1.


Then use a better one. (In particular, assuming C++, don't wrap printf-type stuff, because it's not typesafe. Instead, work with iostreams.)

Share this post


Link to post
Share on other sites
Sign in to follow this  

  • Advertisement