health bar not displaying right

Posted by Joeyfogas on Wed 21 Mar 2018 05:47 AM — 9 posts, 30,858 views.

#0
can someone help out and tell me why this code doesn't display right?

it is supposed to be a healthbar based on health percentage



send_to_char("[",ch);
int value;
value = ch->hit/ch->max_hit ;
for (start = 1; start <= 100; start++ )

{
if (start <= value * 100)
send_to_char("|",ch);
else
send_to_char(" ",ch);
}
send_to_char("] \r",ch);

it seems to only display blank unless I am at 100% health.
Amended on Wed 21 Mar 2018 05:49 AM by Joeyfogas
#1

output at 50% should resemble:
[|||||||||||||||               ]


i guess the forum didnt like all the spaces... won't let me edit it seems either
#2
i realize the written code was ugly so I cleaned it up.. it now reads:


send_to_char("[",ch);
int value = 100 * (ch->hit/ch->max_hit) ;
   for (start = 1; start <= 100; start++ )
     {
     if (start <= value )
       send_to_char("|",ch);
     else
       send_to_char(" ",ch);
    }
   send_to_char("]  \r",ch);



but same issue
Amended on Wed 21 Mar 2018 06:39 AM by Nick Gammon
Australia Forum Administrator #3
An integer only holds whole numbers, so for example if hit is 50 and max_hit is 100, then hit/max_hit will be zero (until hit is the same as max_hit).

You might want to work with floats here.
#4

send_to_char("[",ch);
float value = 100 * (ch->hit/ch->max_hit) ;
                          for (start = 1; start <= 100; start++ )

                                {
                          if (start <= value )
                                        send_to_char("|",ch);
                                  else
                                        send_to_char(" ",ch);
                                }
                    send_to_char("]  \r",ch);



still gives same outcome:

----------------------------------------------------------------------------
You are a level 65 Elf Adventurer from O'ran Thalore.
Your TIER rank is 0.

STR : 10 Attack: 0 Health: 300/600
[
]
AGI : 180 Defense: 412
INT : 180 M. Def: 0
LCK : 180



(disregard the obvious need for a length of output fix.. more focused on getting it working first)
Australia Forum Administrator #5
See this: http://www.gammon.com.au/forum/?id=12146

Making it float is too late.

Try changing 100 to 100.0
#6
nope :(

didn't work
Australia Forum Administrator #7
OK, the brackets didn't help. Try:


float value = 100.0 * ((float) ch->hit / (float) ch->max_hit) ;
Amended on Wed 21 Mar 2018 08:28 AM by Nick Gammon
#8
woohoo! that got it. THANK YOU!!