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;
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
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: