debug help
Mar 29, 2013 at 2:27am UTC
hi im writing a program to calculate the bmi number,
atm i coded the choice between imperial and metric,when you choose imperial
it sends it off to another function and then converts is to metric units and calculates the bmi, the problem im having is it outputs a number like -1.#IND
source code:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
#include <iostream>
using namespace std; // BMI CALC
// Formula: bmi=(weight lbs/(height inch^2))*703
float inch_conv(float inches)
{
float inch;
inch * 2.54;
return (inch);
}
float lbs_conv(float lbs)
{
float lbkg;
lbkg * 0.454;
return (lbkg);
}
float bmi_value(float weight,float height) // calculates bmi
{
return (weight/(height*height));
}
// integers needed: float lbs, float height, float bmi
int main()
{
int numtype; // choose between imp & metric
int repeat; // choose to make another bmi calc
float wght;
float hght;
float bmi_num; // bmi figure
float new_kg; // for imperial conversion
float new_cent; // for imperial conversion
repeat=1;
cout << " Welcome to the BMI calculator!!!" << endl;
do
{
cout << " For metric press 1 for imperial press 2..." << endl;
cin >> numtype;
if (numtype==1)
{
cout << " Please enter your weight in kg" << endl;
cin >> new_kg;
cout << " Now your height in centimeters" << endl;
cin >> new_cent;
}
else
{
cout << " Please enter your weight in lbs" << endl;
cin >> wght;
cout << " Now your height in inches" << endl;
cin >> hght;
new_kg=lbs_conv(wght);
new_cent=inch_conv (hght);
}
bmi_num= bmi_value(new_kg, new_cent);
cout << endl
<< " Your bmi is " << bmi_num << endl
<< endl << " Would you like another calculation? Press 1 for yes or 2 for no." << endl;
cin >> repeat;
}
while (repeat==1);
system("PAUSE" );
}
Mar 29, 2013 at 4:05am UTC
I think you should start by changing this (does this actually compile?):
1 2 3 4 5 6 7 8 9 10 11 12 13
float inch_conv(float inches)
{
float inch;
inch * 2.54;
return (inch);
}
float lbs_conv(float lbs)
{
float lbkg;
lbkg * 0.454;
return (lbkg);
}
to this:
1 2 3 4 5 6 7 8 9
float inch_conv(float inches)
{
return (inches * 2.54);
}
float lbs_conv(float lbs)
{
return (lbs * 0.454);
}
If it actually lets you compile with the version you posted, then it is returning an uninitialized variable.
Mar 29, 2013 at 6:26am UTC
thanks for the help, i scrapped the conversion functions and opted to make an extra function for the imperial bmi formula:
1 2 3 4 5 6 7 8 9 10 11
float bmi_imp(float weight,float height) // calculates bmi
{
return (weight/(height*height)*703);
}
float bmi_metric(float weight,float height) // calculates bmi
{
return (weight/(height*height));
}
it works fine now =]
Topic archived. No new replies allowed.