C++自动舍入
C++ Auto Rounding
我在下面的代码会自动舍入输入。我没有看到任何可以在任何地方舍入输入的功能。有人可以看看吗?
#include <iostream>
#include <string>
#include <sstream>
using namespace std;
int main()
{
string input = "";
int weight = 0;
int height = 0;
int bmi = 0;
while (true)
{
cout << "Enter weight: ";
getline(cin, input);
// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> weight)
break;
cout << "Invalid number, please try again" << endl;
}
while (true)
{
cout << "Enter height: " << endl;
getline(cin, input);
// This code converts from string to number safely.
stringstream myStream(input);
if (myStream >> height)
break;
cout << "Invalid number, please try again" << endl;
}
bmi = height * height;
bmi = weight/bmi;
if(bmi > 25)
{
cout << "Overweight" << endl;
}
else if(bmi < 18.5)
{
cout << "Underweight" << endl;
}
else
{
cout << "Normal weight" << endl;
}
}
您遇到称为整数截断的问题。这可以通过使用浮动类型(例如 double
或 float
)轻松修复。
相关文章:
- C++将浮点指针值舍入为小数位数
- Boost::posix_time::ptime舍入到给定的分钟数
- 浮点定向舍入和优化
- 为什么输出精度没有正确舍入?
- 使用设置精度时如何阻止数字向上舍入?
- 如何在使用 MPFR 时在提升多精度中设置舍入模式
- OpenCV 细分一致的 Rect2f 到 Rect2i 舍入
- C++ 中的舍入函数
- 舍入 QDecDouble 值,精度最多为两个字符
- 将大 int 转换为浮点数,而不舍入 c++
- 舍入错误检测
- 根据浮点符号对浮点进行舍入的最简单方法是什么
- 将整数舍入到另一个整数的最接近的倍数
- Sizeof 舍入到对齐方式,但编译器仍将对象放在剩余的字节中
- 避免将 Int 转换为双重类型转换舍入
- 浮点数学运算后舍入不一致
- 浮动到绳子而无需舍入
- C++长双精度格式而不舍入
- 是否可以为 AVR 编写一个 constexpr 舍入函数?
- Time_T舍入到几分钟