[Home] [Downloads] [Search] [Help/forum]


Register forum user name Search FAQ

Gammon Forum

[Folder]  Entire forum
-> [Folder]  SMAUG
. -> [Folder]  SMAUG coding
. . -> [Subject]  Ahh more exact number ranges...

Ahh more exact number ranges...

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page


Pages: 1 2  

Posted by Gtr   (38 posts)  [Biography] bio
Date Sat 10 Jul 2004 11:25 PM (UTC)
Message
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.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #1 on Sat 10 Jul 2004 11:41 PM (UTC)
Message
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.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Gtr   (38 posts)  [Biography] bio
Date Reply #2 on Sat 10 Jul 2004 11:47 PM (UTC)
Message
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.
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #3 on Sun 11 Jul 2004 03:54 AM (UTC)
Message
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.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Gtr   (38 posts)  [Biography] bio
Date Reply #4 on Sun 11 Jul 2004 04:17 AM (UTC)

Amended on Sun 11 Jul 2004 05:29 AM (UTC) by Gtr

Message
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;
[Go to top] top

Posted by Meerclar   USA  (733 posts)  [Biography] bio
Date Reply #5 on Sun 11 Jul 2004 04:33 AM (UTC)
Message
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.

Meerclar - Lord of Cats
Coder, Builder, and Tormenter of Mortals
Stormbringer: Rebirth
storm-bringer.org:4500
www.storm-bringer.org
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #6 on Sun 11 Jul 2004 05:35 AM (UTC)
Message
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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

Posted by Gtr   (38 posts)  [Biography] bio
Date Reply #7 on Sun 11 Jul 2004 06:48 AM (UTC)

Amended on Sun 11 Jul 2004 06:55 AM (UTC) by Gtr

Message
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.


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.

[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #8 on Sun 11 Jul 2004 08:57 AM (UTC)

Amended on Sun 11 Jul 2004 08:59 AM (UTC) by Greven

Message
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.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by Gtr   (38 posts)  [Biography] bio
Date Reply #9 on Sun 11 Jul 2004 10:47 AM (UTC)
Message
success! thanks.
[Go to top] top

Posted by Nick Cash   USA  (626 posts)  [Biography] bio
Date Reply #10 on Sun 11 Jul 2004 07:05 PM (UTC)
Message
Seems like the compiler would have complained about something like that, yes?

~Nick Cash
http://www.nick-cash.com
[Go to top] top

Posted by Zeno   USA  (2,871 posts)  [Biography] bio
Date Reply #11 on Sun 11 Jul 2004 07:51 PM (UTC)
Message
It should have. I don't think ifchecks are suppose to end with ;, at least I've never seen any.

Zeno McDohl,
Owner of Bleached InuYasha Galaxy
http://www.biyg.org
[Go to top] top

Posted by Greven   Canada  (835 posts)  [Biography] bio
Date Reply #12 on Sun 11 Jul 2004 08:07 PM (UTC)
Message
Well, I beleive that you need a compile flag for gcc to complain about it, anyways. The pedantic flag, if I remember correctly.

Nobody ever expects the spanish inquisition!

darkwarriors.net:4848
http://darkwarriors.net
[Go to top] top

Posted by Gtr   (38 posts)  [Biography] bio
Date Reply #13 on Mon 12 Jul 2004 01:44 PM (UTC)
Message
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.
[Go to top] top

Posted by Nick Gammon   Australia  (22,975 posts)  [Biography] bio   Forum Administrator
Date Reply #14 on Tue 13 Jul 2004 04:22 AM (UTC)
Message
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.


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.

- Nick Gammon

www.gammon.com.au, www.mushclient.com
[Go to top] top

The dates and times for posts above are shown in Universal Co-ordinated Time (UTC).

To show them in your local time you can join the forum, and then set the 'time correction' field in your profile to the number of hours difference between your location and UTC time.


45,618 views.

This is page 1, subject is 2 pages long: 1 2  [Next page]

It is now over 60 days since the last post. This thread is closed.     [Refresh] Refresh page

Go to topic:           Search the forum


[Go to top] top

Quick links: MUSHclient. MUSHclient help. Forum shortcuts. Posting templates. Lua modules. Lua documentation.

Information and images on this site are licensed under the Creative Commons Attribution 3.0 Australia License unless stated otherwise.

[Home]


Written by Nick Gammon - 5K   profile for Nick Gammon on Stack Exchange, a network of free, community-driven Q&A sites   Marriage equality

Comments to: Gammon Software support
[RH click to get RSS URL] Forum RSS feed ( https://gammon.com.au/rss/forum.xml )

[Best viewed with any browser - 2K]    [Hosted at HostDash]