我想在多个函数C ++中使用一个函数的变量

时间:2014-11-13 01:07:11

标签: c++ function variables return

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

这些是该任务的说明。我编写代码没有问题,但我在必要的函数中使用grosspay变量时遇到了麻烦。非常感谢在修改我的代码时提供一些帮助。

#include <iostream>
using namespace std;

double computeGross(double, double);
double computeDeductions(double);
void computeNet();
void validateHours();
void validateWage();

int main()
{
    double hours, wage;
    cout << "Please input the hourly wage of the employee. " << endl;
    cin >> wage;
    cout << "Please input the number of hours the employee worked." << endl;
    cin >> hours;
    computeGross(hours, wage);
    computeDeductions(grosspay);
    computeNet();
    validateHours();
    validateWage();
    return 0;
}

double computeGross(double hours, double wage)
{
    double grosspay = hours*wage;
    cout << "The employee's gross pay is: " << " " << grosspay << endl;
}

double computeDeductions(double grosspay)
{
    double total, sstotal, taxtotal;
    double socialsecurity = .175;
    double tax = .2;
    double reducedtax = .1;
    sstotal = grosspay*socialsecurity;
    if (grosspay < 2500)
    {

        taxtotal = grosspay*reducedtax;
        total = sstotal + taxtotal;
        cout << "The total amount of deductions is: " << total << endl;
    }
    else
    {
        taxtotal = grosspay*tax;
        total = sstotal + taxtotal;
        cout << "The total amount of deductions is: " << total << endl;
    }
}
void computeNet()
{
    double  netsalary;
    netsalary = grosspay - deductions;
    cout << "The net salary is: " << netsalary << endl;
    cout << "The gross salary is: " << grosspay << endl;
    cout << "The deductions total: " << deductions << endl;
}
void validateHours()
{
    if (hours > 150 || hours < 0)
    {
        cout << "Invalid number of hours." << endl;
    }
}
void validateWage()
{
    if (wage > 200 || wage < 0)
    {
        cout << "Invalid wage." << endl;
        return ;
    }
}

0 个答案:

没有答案