C ++自动舍入

空设备

我下面的代码是自动舍入输入。我看不到任何函数可以将输入四舍五入。有人可以看看吗?

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

您遇到一个称为整数截断的问题可以使用浮点类型(例如double或)轻松解决此问题float

本文收集自互联网,转载请注明来源。

如有侵权,请联系[email protected] 删除。

编辑于
0

我来说两句

0条评论
登录后参与评论

相关文章