使用C++函数计算员工的净工资
Computing net salary of employees using functions - C++
以下是我迄今为止所做的工作。我似乎不明白自己做错了什么。我认为我的输入验证函数也不正确。最初的问题是:
编写一个程序——一个计算员工净工资的程序。程序应提示用户输入工作小时数和小时工资。员工支付其总工资的17.5%作为社会保障缴款。所有员工的工资总额都要缴纳20%的税。总工资低于2500的员工,应按其总工资缴纳10%的税。您的程序应该使用以下功能;
a。computeGross:此函数根据工作小时数和小时工资计算工资总额。计算出的工资总额将返回到主函数。
b。compute扣除额:此函数接受总工资作为输入,计算社会保障和税收扣除额,并将总扣除额返回到主函数
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 150n";
}
}
// 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 200n";
}
}
首先,您必须实际调用主函数中的函数,以便它们执行任何操作。
例如:
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;
//you have to actually call your functions lol:
validateHours (hoursWorked);
validateWage(hourlyWage);
grossPay = computeGross(grossPay, hoursWorked, hourlyWage);
deductions = computeDeductions(grossPay);
netSalary = computeNet(netSalary,grossPay, deductions );
// Output the results
cout << fixed << setprecision(2)
<< "The net salary is: $" << netSalary << endl;
return 0;
}
此外,当您在主函数之前声明函数时,您必须确保它们在以后实际创建函数体时具有相同数量的参数。
例如:你在一开始就定义了这一点:
double computeNet(double grossPay, double deductions);
然后,稍后在制作函数体时会出现以下情况:
double computeNet(double netSalary, double grossPay, double deductions)
您也不应该在imo中定制用户定义的函数,但您应该可以完成此任务。你了解参考变量了吗?看起来你正在尝试做的一些事情可能会从中受益。
编辑:我想我修复了它:
#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;
//you have to actually call your functions lol:
validateHours (hoursWorked);
validateWage(hourlyWage);
grossPay = computeGross(hoursWorked, hourlyWage);
deductions = computeDeductions(grossPay);
netSalary = computeNet(grossPay, deductions );
// 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 hoursWorked, double hourlyWage)
{
return hoursWorked * hourlyWage;
}
// computeDeductions() function - gets salary and calculates deductions
double computeDeductions(double grossPay)
{
double deductions;
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 grossPay, double deductions)
{
double netSalary;
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 150n";
}
}
// 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 200n";
}
}
您从未实际调用过任何函数。在收到用户的最终输入后,应该开始调用函数来计算结果,然后它就可以工作了。
它只打印出0的原因是,您将netSalary设置为0,然后从未将变量操作为任何其他值。
- "error: no matching function for call to"构造函数错误
- 什么时候调用组成单元对象的析构函数
- 继承函数的重载解析
- 为什么随机数生成器不在void函数中随机化数字,而在main函数中随机化
- C++模板来检查友元函数的存在
- 递归函数计算序列中的平方和(并输出过程)
- 对RValue对象调用的LValue ref限定成员函数
- C++17复制构造函数,在std::unordereded_map上进行深度复制
- 将数组作为参数传递给函数安全吗?作为第三方职能部门,可以探索他们想要的之外的其他元素
- 在C++STL中是否有Polyval(Matlab函数)等价物?
- 为什么使用 "this" 指针调用派生成员函数?
- 将对象数组的引用传递给函数
- 函数调用中参数的顺序重要吗
- 函数向量_指针有不同的原型,我可以构建一个吗
- 使用不带参数的函数访问结构元素
- 代码在main()中运行,但在函数中出现错误
- 内置函数可查看CPP中的成员变量
- 如何获取std::result_of函数的返回类型
- 如何在c++中为模板函数实例创建快捷方式
- 如果C++类在类方法中具有动态分配,但没有构造函数/析构函数或任何非静态成员,那么它仍然是POD类型吗