gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-28 02:17:51 32 4
gpt4 key购买 nike

以下是我目前所拥有的。我似乎无法弄清楚我做错了什么。我认为我拥有的输入验证功能也不正确。原来的问题是:

编写一个程序来计算雇员的净工资。该程序应提示用户输入工作小时数和小时工资。员工缴纳其工资总额的 17.5% 作为社会保障缴款。所有员工都为其工资总额缴纳 20% 的税。工资总额低于 2500 的员工,按其工资总额缴纳 10% 的税。您的程序应使用以下功能;

一个。 computeGross :此函数根据工作小时数和小时工资计算总工资。计算出的总工资返回给主函数。

computeDeductions:此函数接受总工资作为输入,计算社会保障和税收减免并将总扣除额返回给主函数

computeNet:此函数接受总工资、扣除额作为输入并打印出总工资、总扣除额和净工资。

validateHours:此函数用于输入验证。它确定用户提供的小时数是否有效。支付期间的工作小时数不能超过 150 小时,也不能为负数。

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";
}
}

最佳答案

首先,您必须在主函数中实际调用函数,以便它们执行任何操作。

例如:

    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)

在我看来,您也不应该在用户定义的函数中进行 cout,但您应该可以接受此分配。你了解引用变量了吗?看起来您正在尝试做的一些事情可以从中受益。

编辑:我想我修复了它:

#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 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";
}
}

关于c++ - 使用函数计算员工的净工资 - C++,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33490351/

32 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com