用户输入给出错误的结果
User input giving wrong results
好吧,我在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。将其更改为双精度(浮点数)将解决您的问题。我测试了好几次,它修复了你的代码。
相关文章:
- 在没有定义返回类型的函数中返回布尔值,并将结果保存在无错误的char编译中-为什么
- C++ 随机数生成器:尝试将结果作为向量获取,但通过制作 void 函数来执行此操作而出现错误
- Static_cast转换为错误的数据类型,但结果仍然正确?
- C++ 犰狳库中的sort_index()函数给出了错误的结果
- C++:使用类在向量中搜索特定元素时,我得到了错误的结果
- 检查 n2 是否是 n1 的倍数后结果错误,但根本没有错误
- 为什么我的性能基准给了我错误的结果?
- 使用具有默认参数的函数模板进行 decltype 会使结果混乱(一个有趣的问题或 gcc 的错误)
- 斐波那契检验(C++)的结果错误
- 在发布版本中划分两个双精度的结果错误
- 2D FFT后的图像结果错误
- 计算文本文件中的单个字母并打印结果 - 错误的输出 C++
- C++字符和 int 之间的转换,结果错误
- char*与std字符串的比较结果错误
- 斯波吉购物结果错误
- 超载"+"操作员,结果错误?
- 执行高斯消去程序时结果错误
- hash_djb2 PHP结果错误
- 按位操作比较结果错误
- 使用'if'语句和"<<"时的结果错误,'>>'运算符比较 3 个整数