c++ - 你如何得到一个方程式来输出小数形式的答案?

标签 c++

我正在为类作业做作业,我几乎什么都得到了。它只是应该获取盒子的宽度和高度并输出面积以及需要多少油漆。我可以获得前三个部分,但它只会告诉我需要多少油漆,如果它是 400 的倍数(用于确定需要多少油漆的数字)。

我目前有这个:

#include <iostream>

using namespace std;

int main()
{
    int wallHeight, wallWidth, wallArea;

      cout << "Please enter the height of the wall (feet): ";
    cin >> wallHeight;

    cout << endl << "Please enter the width of the wall (feet): ";
    cin >> wallWidth;

    wallArea = wallHeight * wallWidth;
    const double paintNeeded = wallArea / 400;

    cout << endl << "The area of the wall is: " << wallArea << " square feet. "
         << "This will require about " << paintNeeded << " gallons of paint." << endl;

    return 0;
}

假设我输入的是 24x2 的墙。它会说面积是 48,但它需要 0 加仑的油漆,我只想说它需要 0.12 加仑。

感谢您的帮助。

最佳答案

wallArea 转换为 double:

const double paintNeeded = (double)wallArea / 400;

关于c++ - 你如何得到一个方程式来输出小数形式的答案?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42101361/

相关文章:

c++ - 交互设备的定义

c++ - 使用 read() 时,TCP 流中包含哪些 header ?

c++ - 对同一原子变量混合放松访问和获取/释放访问如何影响同步?

c++ - 通过引用传递 "advanced"概念?

c++ - 回滚 XML 功能

c++ -/MT 和/clr 不兼容

c++ - 为什么下标运算符 C++ 经常成对出现?

c++ - 将整数传递给请求引用的函数

java - 如何切换 Eclipse 视角?

具有内存位置的 C++ 删除运算符