【问题标题】:Computing net salary of employees using functions - C++使用函数计算员工的净工资 - C++
【发布时间】: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() 调用任何函数 :) 没有调试器可以帮助他调试他没有调用的代码 :)
  • 不,但应该会让缺少所说的电话变得非常明显。

标签: c++ function


【解决方案1】:

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

例如:

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

此外,当您在 main 函数之前声明函数时,您必须确保在稍后实际创建函数体时它们具有相同数量的参数。

例如: 你在一开始就定义了这个:

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

【讨论】:

    【解决方案2】:

    您从未真正调用过您的任何函数。在收到用户的最终输入后​​,您应该开始调用函数来计算结果,然后它应该可以工作了。

    它只打印出 0 的原因是因为您将 netSalary 设置为 0,然后从未将变量操作为任何其他值。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-08-01
      • 1970-01-01
      • 2021-12-17
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多