用户输入给出错误的结果

User input giving wrong results

本文关键字:结果 错误 出错 输入 用户      更新时间:2023-10-16

好吧,我在taxRate的用户输入方面遇到了问题,当我编译代码时,我得到了错误的结果。

#include <iostream>
#include <iomanip>
using namespace std;
int main()
{
long propertyValue;
long taxRate;
long exemption = 5000;
cout << "What is the actual value of your property?n";
cin >> propertyValue;
cout << "What is the current tax rate?n";
cin >> taxRate;
cout << "You will pay an annual property tax of ";
cout << (propertyValue - exemption) * taxRate / 100.00; 
cout << " on your propertyn";
cout << "and your quarterly tax bill will be; ";
cout << (propertyValue - exemption) * taxRate / 100.00 / 4 << endl;
system("pause");
return 0;
}

这是代码中的一个简单错误,我将解释原因
举个例子,PropertyValue为300000,税率为5.6。您使用了年度财产税税率的公式:

(PropertyValue - exemption) * taxRate / 100.00;

你宣布taxRate为长(又称长int)

long taxRate;

300000-5000=295000*5.6<-----你的错误就在这里!记住,long是int的一个属性,所以5.6变成了5。将其更改为双精度(浮点数)将解决您的问题。我测试了好几次,它修复了你的代码。