gpt4 book ai didi

C++ 自动舍入

转载 作者:太空狗 更新时间:2023-10-29 23:36:06 26 4
gpt4 key购买 nike

我下面的代码自动舍入输入。我看不到任何可以在任何地方舍入输入的函数。有人可以看一下吗?

#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;
}
}

最佳答案

您遇到一个名为 integer truncation 的问题.这可以通过使用 float 类型轻松解决,例如 doublefloat

关于C++ 自动舍入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21819136/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com