Как мне получить правильный ИМТ? - PullRequest
1 голос
/ 05 мая 2020

Возникла проблема с моим расчетом для определения ИМТ. Пожалуйста, сообщите мне, где я ошибаюсь, поскольку ответ всегда возвращается как -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;
}

1 Ответ

4 голосов
/ 05 мая 2020

Ваша функция getData() принимает свои параметры по значению , поэтому любые изменения, которые она вносит в них, не отражаются обратно в переменные в main(), поэтому они все равно 0.0 при передаче в calcBMI().

Вместо этого нужно передать параметры по ссылке :

void getData(float &weightP, float &heightP)
...