Screwy variables
Maybe I''m crazy or just overlooking the obvious cause it''s
three in the morning and I haven''t had anything that even remotely resembles sleep for the last 37 hours but this is driven me nuts.
I wrote a function called DieRoll() that returns a random number between 1 and 6, and then I wrote another function that uses the output of that function and calculates a position between 0 and 20, also if the number goes over 20 it is to return to 0 and keep counting.
The code for that function is as follows:
if ((count+=roll) > 20)
{
int difference = (count+=roll) - 20;
count = difference - 1;
}
else
{
count+=roll;
}
return count;
I ran it in the debugger (I''m using VisualC++ 6.0 by the way)
and this function returns the value that it''s supposed to
but all of a sudden when I try to output the variable to a dialog control it comes back with a screwy new value altogether (if DieRoll() returned a 6 and count started off at 0 then all of a sudden, count magically equals 12).
m_iPosition = count;
UpdateData(FALSE);
When I ran it through the debugger it was at UpdateData(FALSE)
that it suddenly changed from 6 to 12, what the bloody frig am I
missing?
Can somebody please help me? It would be greatly appreciated, although I''m sure it''s something ridiculously simple that I have been overlooking.
Well...
You''re aware that every time you use += it changes the first variable? For instance, allow me to demonstrate showing in comments the actual values of count and roll at each step:
DieRoll makes roll = 6.
count = 0 (to start)
if ((count += roll) > 20))
// count = 6
// roll = 6
{
int difference = (count += roll) - 20;
// count = 6 + 6 = 12
// roll = 6
// difference = -8
count = difference - 1;
// count = -8 - 1 = -9;
}
which will do what you expect I guess, I''m not entirely sure what you''re trying to do there, but... take it from the if and throw it through your else...
if ((count += roll) > 20))
// false condition, but... count = 6, roll = 6
else
{
count += roll;
// count = 6+6 = 12
// roll = 6
}
So... that''s where the 12 is coming from.
You need to probably change, if I''m understanding you correctly, the
if ((count += roll) > 20)
to
if ((count + roll) > 20)
so that count isn''t incremented by the value of roll twice during the else.
-fel
I hope that makes sense. *blink*
You''re aware that every time you use += it changes the first variable? For instance, allow me to demonstrate showing in comments the actual values of count and roll at each step:
DieRoll makes roll = 6.
count = 0 (to start)
if ((count += roll) > 20))
// count = 6
// roll = 6
{
int difference = (count += roll) - 20;
// count = 6 + 6 = 12
// roll = 6
// difference = -8
count = difference - 1;
// count = -8 - 1 = -9;
}
which will do what you expect I guess, I''m not entirely sure what you''re trying to do there, but... take it from the if and throw it through your else...
if ((count += roll) > 20))
// false condition, but... count = 6, roll = 6
else
{
count += roll;
// count = 6+6 = 12
// roll = 6
}
So... that''s where the 12 is coming from.
You need to probably change, if I''m understanding you correctly, the
if ((count += roll) > 20)
to
if ((count + roll) > 20)
so that count isn''t incremented by the value of roll twice during the else.
-fel
I hope that makes sense. *blink*
~ The opinions stated by this individual are the opinions of this individual and not the opinions of her company, any organization she might be part of, her parrot, or anyone else. ~
Your getting unexpected numbers back because
if ((count+=roll) > 20)... adds 6 to the "count" variable, and since the result is less then 20... 6 is added again in the "else" statement.
If I am understanding what you want to do... Try this..
if ( ( count + roll ) > 20 )
{
int difference = ( count + roll ) - 20;
count = difference - 1;
}
else
{
count += roll;
}
return count;
Or better yet..
if ( ( count += roll ) > 20 )
{
count -= 21;
}
return count;
Edit:
Whoops didn't see felisandria's reply. I was disconnected while trying to post.
Edited by - Boke on July 5, 2001 2:51:04 AM
if ((count+=roll) > 20)... adds 6 to the "count" variable, and since the result is less then 20... 6 is added again in the "else" statement.
If I am understanding what you want to do... Try this..
if ( ( count + roll ) > 20 )
{
int difference = ( count + roll ) - 20;
count = difference - 1;
}
else
{
count += roll;
}
return count;
Or better yet..
if ( ( count += roll ) > 20 )
{
count -= 21;
}
return count;
Edit:
Whoops didn't see felisandria's reply. I was disconnected while trying to post.
Edited by - Boke on July 5, 2001 2:51:04 AM
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement
Recommended Tutorials
Advertisement