如何获得正确的 BMI?

How do I get BMI correct?

我确定 BMI 的计算有问题。请告诉我哪里出错了,因为答案总是 returns as -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() 中的变量,因此传递给 calcBMI() 时它们仍然是 0.0

您需要通过引用传递参数而不是:

void getData(float &weightP, float &heightP)