我几个月来一直在自学C ++,现在我正在尝试制作薪资系统。这是我的代码:
#include <iostream>
#include <cstdlib>
#include <string>
using namespace std;
void wageCompute (int, int);
int main()
{
int loopTimes=0;
int empNum=100, workHours, otHours, rate, bPay, otPay, grossPay;
string empName, empPos;
cout << "PAYROLL FOR THE MONTH OF MARCH" << endl;
cout << "Employees: " << empNum << endl;
while (empNum>loopTimes)
{
cout << "NAME: ";
cin.ignore();
getline (cin, empName);
cout << "\nPOSITION: ";
getline (cin, empPos);
cout << "\nHOURS WORKED: ";
cin >> workHours;
cout << "\nWAGE PER HOUR: ";
cin >> rate;
bPay = workHours*rate;
cout << "YOUR BASE PAY IS: " << bPay << endl << endl;
cout << "HOURS WORKED OVERTIME: ";
cin >> otHours;
otPay = (1.5*rate)*otHours;
cout << "\nOVERTIME PAY: " << otPay << endl;
grossPay = bPay + otPay;
cout << "GROSS PAY: " << grossPay << endl;
wageCompute(bPay, grossPay);
loopTimes++;
}
return EXIT_SUCCESS;
}
void wageCompute(int bPay, int grossPay)
{
double rate, dedInsurance, dedMedical, totDeduct, netPay, tax;
if (bPay<10001)
{
rate = 0.05;
}
else if (bPay<15001)
{
rate = 0.1;
}
else if (bPay<20001)
{
rate = 0.15;
}
else
{
rate = .2;
}
tax = bPay*rate;
dedInsurance = bPay*0.05;
dedMedical = bPay*0.01;
totDeduct = tax + dedInsurance + dedMedical;
cout << "TAX: " << tax << endl;
cout << "SSS DEDUCTION: " << dedInsurance << endl;
cout << "Medicare DEDUCTION: " << dedMedical << endl;
cout << "TOTAL DEDUCTIONS: " << totDeduct << endl;
netPay = grossPay - totDeduct;
cout << "NET PAY: " << netPay << endl;
}
出错的部分是我输入工作小时数,每小时工资和工作小时数的特定值。该计划检查基本工资是否应扣除适当的税额,我输入的工作时数为160,工资为每小时100,加班费为10。我已经尝试过减少和增加它并且它工作得很好似乎只是这些数字组合是它出错的部分。
输出的屏幕截图:
答案 0 :(得分:0)
你的问题不是很清楚,但我怀疑你在这里看到的是一个众所周知的浮点数限制;容易在基数10中精确表示的数字在基数2中没有精确的表示。一个例子:基数10中的0.1是0.0001100110011 ...在基数2中重复;近似的准确性取决于一个人愿意用它来写多少位。
另一种方法是使用具有已知精度的整数算术,所以说你要计算到最接近一分钱的百分之一(我在这里使用英国货币)。代表£1.01为10100,当你完成val / 10000
是磅,而(val % 10000) / 100
是便士。如果需要,你可以围绕便士的四舍五入实现一些更复杂的规则。