gpt4 book ai didi

c++ - 我如何正确获得BMI?

转载 作者:行者123 更新时间:2023-12-02 09:49:24 29 4
gpt4 key购买 nike

确定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()函数按值获取其参数,因此对其进行的任何修改都不会反射(reflect)回main()中的变量,因此当传递给0.0时,它们仍然是calcBMI()

您需要改为通过引用传递参数:

void getData(float &weightP, float &heightP)

关于c++ - 我如何正确获得BMI?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61606464/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com