BMI prog. help!!!!

Oct 9, 2013 at 4:05pm
So- I'm trying to successfully run a program that calculates your BMI but, although it runs, it is not giving the the correct math. Instead, it gives me the number i've submitted as my weight as an answer. I'm using Visual Studio 2008 and the formula: weight / (height/100)*2 and I must declare the BMI as a double an weight/height as int's. I suspect it has to do with the cast operator.

Here is my code

#include <iostream>
#include <cmath>
using namespace std;
int main()
{
int weight;
int height;
double BMI;


cout<<"You must submit your weight(kg) in order to find your body mass index- please enter your weight now: ";
cin>>weight;

cout<<"\nPlease enter your height in cm: ";
cin>>height;

BMI =weight / ((height/100)*(height/100));
cout<< "\nYour BMI is: "
<< (double)BMI; <<endl;


return 0;


**Any help would be so very..... awesome**
Oct 9, 2013 at 4:09pm
closed account (Dy7SLyTq)
a) you have << (double)BMI; <<endl; notice the two semicolons

b)BMI is double why would you do a cast to it?

c) i would use a pow function instead of doing it like that

edit: and in the future please use ^ to represent power of. thats not what its used for in c++ buts its a pretty common notation. i thought your original formula meant times two, not to the power of two
Last edited on Oct 9, 2013 at 4:10pm
Oct 9, 2013 at 5:25pm
My apologies. I am a beginner. Thank you for the corrections.

How would I use a pow function in this program?

Cleaned up program:

#include <iostream>
#include <cmath>

using namespace std;
int main()
{


int weight;
int height;
double BMI;


cout<<"You must submit your weight(kg) in order to find your body mass index- please enter your weight now: ";
cin>>weight;

cout<<"\nPlease enter your height in cm: ";
cin>>height;


BMI =weight / (height/100)*(height/100);
cout<<"\nYour BMI is: " <<BMI

<<endl;



return 0;
Oct 9, 2013 at 5:33pm
closed account (Dy7SLyTq)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
template<class Type>

Type Pow(Type Base, int Power)

{
	auto Factor = Base;

	for(int Counter = 0; Counter < Power; Counter++)
		Factor *= Base;

}

int main()
{
	//...
	BMI =weight / Pow((height/100), 2);
	//...
}
Oct 9, 2013 at 5:43pm
I wouldn't worry about the Pow class. The way you wrote it is fine with a couple modifications.

The height variable is an int and the constant 100 is an int. When you divide these, you use int division. You want the result to be have a fractional part, so you need to make sure you use division of double values. To do this, make sure at least one of the operands is a double. The simplest way to do this is to replace 100 with 100.0 .

Edit: Oh wait. The denominator must be in parentheses. What you are doing is taking weight, dividing it by height/100 and multiplying it by height/100. The results in weight. So, your expression should be:

BMI = weight / ( (height / 100.0) * (height/100.0) );
Last edited on Oct 9, 2013 at 5:48pm
Oct 9, 2013 at 6:13pm
Aaaaah thank you, doug4! Worked flawlessly.
Topic archived. No new replies allowed.