我在确定BMI的计算中遇到问题。请告诉我我哪里出错了,因为答案总是返回-nan(ind)。我确定问题出在计算本身,因为我删除了displayFitnessResults函数并简化了代码,但仍然收到错误。
#include<iostream>
#include <cmath>
using namespace std;
void getData(float weightP, float heightP)
{
cout << "Enter indivual's wight in kilograms and height in metres: ";
cin >> weightP >> heightP;
}
float calcBMI(float weightP, float heightP)
{
return weightP / (heightP * heightP);
}
void displayFitnessResults(float calcBMI)
{
if (calcBMI < 18.5)
{
cout << "The individuals BIM index is " << calcBMI << " and his/her weight status is underweight";
}
else if (calcBMI >= 18.5 && calcBMI <= 24.9)
{
cout << "The individuals BIM index is " << calcBMI << " and his/her weight status is healthy";
}
else if (calcBMI <= 25 && calcBMI >= 29.9)
{
cout << "The individuals BIM index is " << calcBMI << " and his/her weight status is overweight";
}
else (calcBMI >= 30);
{
cout << "The individuals BIM index is " << calcBMI << " and his/her weight status is obese";
}
}
int main()
{
float weight{}, height{}, BMI{};
cout.setf(ios::fixed);
cout.precision(2);
getData(weight, height);
BMI = calcBMI(weight, height);
displayFitnessResults(BMI);
return 0;
}
您的getData()
函数采用其参数按值”,因此对该参数所做的任何修改都不会反映回main()
中的变量,因此当传递给0.0
时它们仍为calcBMI()
。
您需要传递参数通过引用代替:
void getData(float &weightP, float &heightP)