【发布时间】:2015-11-13 08:21:37
【问题描述】:
我的程序请求两个变量:一个整数和一个浮点数。两者都必须大于 0 且小于 2000。但测试 45 和 0 的输入时,它会接受该值并显示 0.00 的输出,尽管条件需要 (cash > 0) && (balance > 0)。
#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
int cash;
float balance ;
cin >> cash >> balance;
if((cash%5==0) && (cash <= balance) && (cash > 0) && (balance > 0) && (cash <= 2000) && (balance <= 2000))
{
balance = (balance - cash) - 0.50;
cout << fixed;
cout.precision(2);
cout << balance;
}
else if (cash%5 != 0)
{
cout << fixed;
cout.precision(2);
cout << balance;
}
else if (cash > balance)
{
cout << fixed;
cout.precision(2);
cout << balance;
}
return 0;
}
【问题讨论】:
-
您的代码没有输入第一个
if。它进入最后一个,即else if (cash > balance)。这不是故意的吗? -
投票结束,为什么这段代码不起作用。请使用 printfs 观察程序状态,然后生成问题的最小示例。
标签: c++ error-correction