【发布时间】:2018-02-17 22:36:27
【问题描述】:
我正在尝试使用double 为我的weight_Fee 获取输出,但我似乎无法获得正确的值。我试过使用float,但我也无法让它工作。
我的目标是获得一个包含两位小数的输出值,就像我要计算成本一样,但我每次都得到 0.00。
我是 C++ 新手,所以如果有人能告诉我我做错了什么,那将是一个很大的帮助。谢谢。
#include <iostream>
#include <iomanip>
#include <cstdlib>
#include <ctime>
using namespace std;
int main() {
double animal_Weight;
double weight_Fee = .5 * animal_Weight;
cout << "In rounded poundage, how much does your animal weigh? ";
cin >> animal_Weight;
cout << setprecision (2) << fixed << weight_Fee;
return 0;
}
【问题讨论】:
-
变量
animal_Weight是未定义的,可以被编译器或操作系统初始化为任何值,或者内存中的任何值。 -
另外,输入数据后,
weight_Fee不会再次计算。我建议将weight_Fee的定义移到输入animal_Weight之后。 -
语句按顺序执行。您如何期望乘法在您要求输入之前起作用?
标签: c++ variables floating-point double output