Hello, I am very new to c++ i started a couple days ago, today I tried to make a BMI calculator and cannot get the formula to work. The Calculator is text base.
any help would be very much appreciated.
Source Code::
#include <iostream>
using namespace std;
int BMI(int W, int H, int iH)
{
cout << "\n\nBMI() recieved your weight and height " << endl;
cout << "Calculating your BMI ......\n";
return (W * 703 / H * H * iH * iH);
}
int main()
{
cout << "BMI Calculator version 1. // By Nathan Greenberg\n\n";
unsigned short int weight, height, iHeight, tBMI;
cout << "Enter your Weight: ";
cin >> weight;
cout << "Enter your Height(feet): ";
cin >> height;
cout << "Enter your Height(inches): ";
cin >> iHeight;
tBMI= BMI(weight, height, iHeight);
cout << "Your BMI is " << tBMI;
cout << "\n\n\n\nThank you for using :::NG Software:::";
char response;
cin >> response;
return 0;
}
what you want to do is multiply the height they enter in feet by 12 and add it to iHeight. make your function body more like this:
1 2 3 4 5 6 7 8 9 10
double BMI(double W, double H, double iH)
{
cout << "\n\nBMI() recieved your weight and height " << endl;
cout << "Calculating your BMI ......\n";
iH += H * 12;
return (W * 703) / (iH * iH);
}
lastly BMI's are rarely whole numbers so i would change all the variables in your program to type double. i did this for the function, and you should do the same in main.
H*H*iH*iH doesn't get the height in inches. Multiply the height by twelve and add inches to that. Try:
1 2 3 4 5 6 7
int BMI(int W, int H, int iH)
{
cout << "\n\nBMI() recieved your weight and height " << endl;
cout << "Calculating your BMI ......\n";
iH += H*12;
return ((W*703) / (iH*iH));
}
BTW, unsignedshortint is unnecessary; just use unsigned (in the BMI() args as well).
theres no need for the variable to be short. once again, i reccommended changing everything to type double because BMI's are almost always decimal values, so you want to more precision by making your variables doubles.