[英]C++ Auto Rounding
My code down below is automatically rounding the input. 我下面的代码会自动舍入输入。 I dont see any function to round the input anywhere. 我没有看到任何功能在任何地方围绕输入。 Can someone take a look? 有人可以看看吗?
#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;
}
}
You are experiencing a problem called integer truncation . 您遇到了一个称为整数截断的问题。 This can easily be fixed by using a floating type, such as double
or float
. 这可以通过使用浮动类型(例如double
或float
轻松修复。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.