![]() |
|
![]() |
|
|
Thread Tools | Display Modes |
|
|
#1 |
|
Newbie
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0
![]() |
Comparing doubles issue (I think...). Help!
For my game, I have made a text health bar, here is how I did it:
void healthbars()
{
if (maxhp == hp)
cout << "|---------------------|" <<endl;
else if ((hp/maxhp*100) >=95 && (hp/maxhp*100) <=99)
cout << "|-------------------- |" <<endl;
else if ((hp/maxhp*100) >=90 && (hp/maxhp*100) <=94)
cout << "|------------------- |" <<endl;
else if ((hp/maxhp*100) >=85 && (hp/maxhp*100) <=89)
cout << "|------------------ |" <<endl;
else if ((hp/maxhp*100) >=80 && (hp/maxhp*100) <=84)
cout << "|----------------- |" <<endl;
else if ((hp/maxhp*100) >=75 && (hp/maxhp*100) <=79)
cout << "|---------------- |" <<endl;
else if ((hp/maxhp*100) >=70 && (hp/maxhp*100) <=74)
cout << "|--------------- |" <<endl;
else if ((hp/maxhp*100) >=65 && (hp/maxhp*100) <=69)
cout << "|-------------- |" <<endl;
else if ((hp/maxhp*100) >=60 && (hp/maxhp*100) <=64)
cout << "|------------- |" <<endl;
else if ((hp/maxhp*100) >=55 && (hp/maxhp*100) <=59)
cout << "|------------ |" <<endl;
else if ((hp/maxhp*100) >=50 && (hp/maxhp*100) <=54)
cout << "|----------- |" <<endl;
else if ((hp/maxhp*100) >=45 && (hp/maxhp*100) <=49)
cout << "|---------- |" <<endl;
else if ((hp/maxhp*100) >=40 && (hp/maxhp*100) <=44)
cout << "|--------- |" <<endl;
else if ((hp/maxhp*100) >=35 && (hp/maxhp*100) <=39)
cout << "|-------- |" <<endl;
else if ((hp/maxhp*100) >=30 && (hp/maxhp*100) <=34)
cout << "|------- |" <<endl;
else if ((hp/maxhp*100) >=25 && (hp/maxhp*100) <=29)
cout << "|------ |" <<endl;
else if ((hp/maxhp*100) >=20 && (hp/maxhp*100) <=24)
cout << "|----- |" <<endl;
else if ((hp/maxhp*100) >=15 && (hp/maxhp*100) <=19)
cout << "|---- |" <<endl;
else if ((hp/maxhp*100) >=10 && (hp/maxhp*100) <=14)
cout << "|--- |" <<endl;
else if ((hp/maxhp*100) >=5 && (hp/maxhp*100) <=9)
cout << "|-- |" <<endl;
else if ((hp/maxhp*100) >=1 && (hp/maxhp*100) <=4)
cout << "|- |" <<endl;
else
cout << "| |" <<endl;
cout <<hp<<"/"<<maxhp<<endl<<endl;
}So basically, each "-" represents 5 percent of health. (hp/maxhp*100) figures out the current percentage. 40/50*100 = 80% Well it doesnt work "exactly" like i want it to. I put it in a loop to test it out... while (hp!=0)
{
healthbars();
hp--;
Sleep(80);
}When I execute it, it goes down from 50 to 0, showing the health bar go down, but when it gets to 7/50 (14/100 if I change it to 100/100) it skips that else if and goes down to the else, showing 0 health, why? A problem I had before is that I had the hp and max hp variables as ints, which i figured out didnt work because of the decimal calculation. So is the problem at 7 something to do with a double comparison? |
|
|
|
|
|
#2 |
|
Programming Guru
![]() |
try making the formula more explicit so have it as
((hp/maxhp)*100) possibly? |
|
|
|
|
|
#3 |
|
I eat cake for breakfast.
![]() ![]() ![]() ![]() Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9
![]() |
I wouldn't do it like that at all - I'd just calculate the length:
void healthbars()
{
float percentagehealth = (hp * 100) / (maxhp * 100);
cout << "|";
for (int i = 0; i < (percentagehealth * 21) - (percentagehealth * 100 % 5); i++)
{
cout << "-";
}
cout << "|" << endl;
cout << hp << "/" << maxhp << endl << \endl;
} |
|
|
|
![]() |
| Bookmarks |
| Currently Active Users Viewing This Thread: 1 (0 members and 1 guests) | |
| Thread Tools | |
| Display Modes | |
|
|