使用C++函数计算员工的净工资

Computing net salary of employees using functions - C++

本文关键字:C++ 函数 计算 使用      更新时间:2023-10-16

以下是我迄今为止所做的工作。我似乎不明白自己做错了什么。我认为我的输入验证函数也不正确。最初的问题是:

编写一个程序——一个计算员工净工资的程序。程序应提示用户输入工作小时数和小时工资。员工支付其总工资的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,然后从未将变量操作为任何其他值。