【发布时间】:2015-11-03 02:15:54
【问题描述】:
以下是我目前所拥有的。我似乎无法弄清楚我做错了什么。我认为我拥有的输入验证功能也不正确。原来的问题是:
编写一个程序来计算员工的净工资。程序应提示用户输入工作小时数和小时工资。员工支付其工资总额的 17.5% 作为社会保障缴款。所有员工按其总工资缴纳 20% 的税。工资总额低于 2500 的员工,按其工资总额缴纳 10% 的税。您的程序应使用以下功能;
一个。 computeGross :此函数根据工作小时数和小时工资计算总工资。计算出来的工资总额返回给主函数。
b. computeDeductions:此函数接受总工资作为输入,计算社会保障和税收减免,并将总减免返回给主函数
c。 computeNet:该函数接受总工资、扣除额作为输入,并打印出总工资、总扣除额和净工资。
d。 validateHours:此函数用于输入验证。它确定用户提供的小时数是否有效。一个支付周期内的工作小时数不能超过 150 小时,并且不能为负数。
e。 validateWage:此函数用于输入验证。它确定用户提供的小时工资是否有效。时薪不能超过200,不能为负数。
#include <iostream>
#include <iomanip>
using namespace std;
// Declare Functions
double computeGross(double hoursWorked, double hourlyWage);
double computeDeductions(double grossPay);
double computeNet(double grossPay, double deductions);
void validateHours(double hoursWorked);
void validateWage(double hourlyWage);
int main()
{
// Declare Variables
double hoursWorked = 0;
double hourlyWage = 0;
double grossPay = 0;
double deductions = 0;
double netSalary = 0;
// Get the hours worked and hourly wage
cout << "Please enter the amount of hours worked (HH.MM): " << endl;
cin >> hoursWorked;
cout << "Please enter in your hourly wage: $" << endl;
cin >> hourlyWage;
// Output the results
cout << fixed << setprecision(2)
<< "The net salary is: $" << netSalary << endl;
return 0;
}
// compteGross() function - get gross salary based on hours worked and hourly wage.
double computeGross(double grossPay, double hoursWorked, double hourlyWage)
{
return grossPay = hoursWorked * hourlyWage;
}
// computeDeductions() function - gets salary and calculates deductions
double computeDeductions(double deductions, double grossPay)
{
if(grossPay < 2500)
{
deductions = (grossPay * .10) * .175;
}
else
{
deductions = (grossPay * .20) * .175;
}
return deductions;
}
// computeNet() function - prints out gross salary,total deductions and net salary
double computeNet(double netSalary, double grossPay, double deductions)
{
netSalary = grossPay - deductions;
cout << "The gross salary is: $" << grossPay << endl;
cout << "The total deductions are: $" << deductions << endl;
cout << "The net salary is: $" << netSalary << endl;
return netSalary;
}
// validateHours() function - input validation; hours worked can;t exceed 150 or be neg.
void validateHours(double hoursWorked)
{
if(hoursWorked < 0 || hoursWorked > 150)
{
cout << "Error! Hours can't be negative or exceed 150\n";
}
}
// validateWage() - Input validation; wage can't exceed 200 or be negative
void validateWage(double hourlyWage)
{
if(hourlyWage < 0 || hourlyWage > 200)
{
cout << "Error! Wage can't be negative or exceed 200\n";
}
}
【问题讨论】:
-
那么您的具体问题是什么?
-
当我运行程序时,我得到 0 作为净工资,不,我的输入不是 0。我试图弄清楚为什么会这样。
-
请花点时间了解如何使用调试器。单步执行代码,检查变量等。
-
@OldProgrammer 你是老前辈,你可能需要眼镜 :) 他没有从 main() 调用任何函数 :) 没有调试器可以帮助他调试他没有调用的代码 :)
-
不,但应该会让缺少所说的电话变得非常明显。