我最近才学会了C ++。我有一个制作BMI计算器的学校项目。不幸的是,它出现的错误超出了我的理解范围。我不确定我是否应该为我的身高和体重变量使用不同的数据类型 - 这应该是双倍吗?
#include <iostream>
using namespace std;
float bmi_calc(int height, int weight){
float bmi_user = weight / height * height;
return bmi_user;
}
int main()
{
int weight_user;
int height_user();
cout << "Enter your weight in kilograms";
cin >> weight_user;
cout << "Enter your height in meters";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
}
答案 0 :(得分:1)
固定代码:
#include <iostream>
using namespace std;
float bmi_calc(float height, float weight) {
return weight / (height * height);
}
int main()
{
float weight_user;
float height_user;
cout << "Enter your weight in kilograms ";
cin >> weight_user;
cout << "Enter your height in meters ";
cin >> height_user;
cout << "Your BMI is " << bmi_calc(height_user, weight_user);
return 0;
}
因为您的身高以米为单位,所以您需要一个浮点数,因为int只能用于整数。也使用正确的括号来使公式正确:)