BMI 计算器错误

BMI calculator bug

本文关键字:错误 计算器 BMI      更新时间:2023-10-16

我正在尝试做一个基本的 BMI 计算器,当我做它的度量方面时它工作正常,但是,英制方面返回错误的答案。

cout << "What is your weight? ";
cin >> weight;
cout << "What is your height in either inches or meters? ";
cin >> height;
cout << "Is that metric or imperial? Type 1 for metric, or 0 for imperial ";
cin >> unit;
if (unit = 1) 
answer = weight / (height * height);
if (unit = 0)
answer = (weight * 703) / (height * height);
cout << "Your BMI is " << answer << endl;
system("PAUSE");
return 0;  

根据维基百科,我认为英制的 BMI 等式是

重量(磅)* 703/(高度以英寸平方为单位)

使用 == 测试相等性 not =

你在 if 语句中使用赋值运算符 "=",这两个运算符都将始终返回 true,因为您可以将 "0" 或 "1" 的值分配给单位。

要测试相等性,您必须使用"=="。

这应该可以解决此问题!(: