Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

Reply
 
Thread Tools Display Modes
Old May 4th, 2005, 8:44 PM   #1
glopal
Newbie
 
Join Date: May 2005
Location: St. Andrews, Manitoba, Canada
Posts: 9
Rep Power: 0 glopal is on a distinguished road
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?
glopal is offline   Reply With Quote
Old May 5th, 2005, 2:55 AM   #2
Berto
Programming Guru
 
Join Date: Aug 2004
Posts: 1,022
Rep Power: 6 Berto is on a distinguished road
Send a message via AIM to Berto Send a message via MSN to Berto
try making the formula more explicit so have it as

((hp/maxhp)*100)

possibly?
Berto is offline   Reply With Quote
Old May 5th, 2005, 10:48 AM   #3
Ooble
I eat cake for breakfast.
 
Ooble's Avatar
 
Join Date: Jul 2004
Location: In my box.
Posts: 4,434
Rep Power: 9 Ooble is on a distinguished road
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;
}
That's probably wrong, but hey...
__________________
Me :: You :: Them
Ooble is offline   Reply With Quote
Reply

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 9:22 AM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC