Hey guys..me with a question again. I notice when I use number ranges..ie ..errr "number_range( 15, 35 );" the numbers arent exact...it goes way lower..and way higher.... then the two numbers...am I using it correctly? or is it suposed to go way above and way below what is set..if so is there anyway to get it to be more exact ...ie only go between 15 and 35.
Ahh more exact number ranges...
Posted by Gtr on Sat 10 Jul 2004 11:25 PM — 18 posts, 70,891 views.
This depends largely on context to be perfectly honest. If you can tell us where you are using these ranges and what they're for we can give you a better answer.
I'm trying to use it for a random heal between the numbers, 15 and 35. No lower then 15, and no higher then 35.
Would it be possible to paste the code? For a heal I can't think of a good reason why the number would deviate from the range paramater, least not offhand in smaug anyway.
The actual code is cluttered to high hell, Heres the gist of it.
if (ch->mlevel == 0);
{
heal = number_range( 1, 5 );
}
if (ch->mlevel == 1);
{
heal = number_range( 5, 10 );
}
if (ch->mlevel == 2);
{
heal = number_range( 10, 15 );
}
victim->hit += heal;
if (ch->mlevel == 0);
{
heal = number_range( 1, 5 );
}
if (ch->mlevel == 1);
{
heal = number_range( 5, 10 );
}
if (ch->mlevel == 2);
{
heal = number_range( 10, 15 );
}
victim->hit += heal;
Cluttered code is an occupational hazard, who knows, we might can even help clean it up a bit. Post what you're trying to do and let's see what we can see. From the looks of what you posted a case structure could make it easier to track anyway but I'd want to see all of it before saying that for certain.
One obvious problem here is that if the ch->mlevel is (say) 3, then the variable "heal" is undefined, and thus could indeed be out of your acceptable range.
At the very least, have:
heal = 0;
at the start of the if tests.
At the very least, have:
heal = 0;
at the start of the if tests.
I just junked the new heal code, it wasnt that important anyway, but then a similar problem arose in another part of my code I tested, so I did this test.
What it should have done was only report test1...if it was less then 35, but it appears it skips right past the if check "if (test1 < 35);.
Now when I used it, I executed it err a few times...and got this.
The number is 17.
The number is 27.
The number is 30.
The number is 18.
The number is 13.
The number is 31.
The number is 46.
The number is 33.
The number is 37.
The number is 44.
The number is 44.
void do_test( CHAR_DATA *ch, char *argument )
{
OBJ_DATA * holding;
int test1;
test1 = 0;
holding = get_eq_char(ch, WEAR_HOLD);
if (holding->value[0] == 5);
{
test1 = 50;
}
if (holding->value[0] == 4);
{
test1 = number_range( 40, 50 );
}
if (holding->value[0] == 3);
{
test1 = number_range( 30, 50 );
}
if (holding->value[0] == 2);
{
test1 = number_range( 20, 50 );
}
if (holding->value[0] == 1);
{
test1 = number_range( 10, 50 );
}
if (test1 < 35);
{
ch_printf(ch, "&wThe number is %d.\n\r", test1 );
return;
}
}
What it should have done was only report test1...if it was less then 35, but it appears it skips right past the if check "if (test1 < 35);.
Now when I used it, I executed it err a few times...and got this.
The number is 17.
The number is 27.
The number is 30.
The number is 18.
The number is 13.
The number is 31.
The number is 46.
The number is 33.
The number is 37.
The number is 44.
The number is 44.
Oh, heh, I see it. After each of your if statements, you have ";". This effectively says "If this is condition is true, do the next statement. That statement is empty, so execute nothing. Now that the if check is done, execute the next block of code in the {}'s, since these are not attached to an if check". Try removing the ; and see what happens.
success! thanks.
Seems like the compiler would have complained about something like that, yes?
It should have. I don't think ifchecks are suppose to end with ;, at least I've never seen any.
Well, I beleive that you need a compile flag for gcc to complain about it, anyways. The pedantic flag, if I remember correctly.
It compiled clean no warnings or anything under cygwin, and linux. Hell I dont even know why I added the ";" orginaly. Once I got past a little hump in the coding process...I just kept going way late into the night maby thats to blame.
I cannot get gcc to give a warning for that case, perhaps it is so possible in standard libraries that they don't do it.
In any case, it is valid C, it is possible to have empty statements, and they can be part of an "if", eg.
This is effectively what you have done. A lint program might pick it up.
You are much better off using a "switch" statement anyway, it looks cleaner:
See how that is shorter and clearer to read? No multiple "if" statements, and you can easily see what happens if the holding value is not 1 to 5.
In any case, it is valid C, it is possible to have empty statements, and they can be part of an "if", eg.
if (a == b)
// empty statement
;
This is effectively what you have done. A lint program might pick it up.
You are much better off using a "switch" statement anyway, it looks cleaner:
switch (holding->value[0])
{
case 1: test1 = number_range( 10, 50 ); break
case 2: test1 = number_range( 20, 50 ); break
case 3: test1 = number_range( 30, 50 ); break
case 4: test1 = number_range( 40, 50 ); break
case 5: test1 = 50; break
default:
test1 = 0;
break;
} // end of switch
See how that is shorter and clearer to read? No multiple "if" statements, and you can easily see what happens if the holding value is not 1 to 5.
Indeed, I just wasnt too comfortable with switch statements yet, but I understand them now that I'v seen them work with somthing I did with somthing else heh, and yeah it looks a hell of alot nicer, thanks nick.
Yeah, good.
Of course, you have a mathematical relationship there so you could write it this way:
(That is, just multiply by 10, that is all you are doing in your switch/if anyway).
Of course, you have a mathematical relationship there so you could write it this way:
if (holding->value[0] >= && holding->value[0] <= 5)
test1 = number_range( holding->value[0] * 10, 50 );
else
test1 = 0;
(That is, just multiply by 10, that is all you are doing in your switch/if anyway).
Hrmm I would have never thought of that, thou I'm well versed in coming up with formulas now. I must admit the switch statement on the other hand, I'm making lots of use of cleaning up all my existing code.
Thanks again Nick.
Thanks again Nick.