0

我需要帮助,我正在尝试制作一个程序,通过考虑一些变量来计算某人的净工资,在这种情况下,我只是为速度设定了常数。

这些变量是:姓名 Mary、工作时间 (35)、时薪 (10.50)、总工资 (367.50)、税收 (@20%=73.50)、PRSI (@2.5% =9.19) 和工会会费 (3.50)。

这些都经过计算以找到净工资。

我认为这很容易,但我不明白我哪里出错了,我的代码是:

#include <iostream>
#include <string>
using namespace std;

double net_pay (double hours, double gpay, double hrate, double dues,double tax, double taxr,double prsir,double prsi);

int main()
{

    string name;
    const double hrate = 10.50;
    const double taxr = .2;
    const double prsir = .025;
    const double dues = 3.50;
    const int hours = 35;
    cout << "enter name";
    cin >> name;

    net_pay ( hours,  gpay,  hrate, dues, taxr, tax, prsi, prsir);
    cout << name;
    cout << net_pay;


return 0;
}

double net_pay (double hours, double gpay, double hrate , double dues , double taxr, double tax , double prsi , double prsir) {

    gpay = hours * hrate;
    tax = gpay * taxr;
    prsi = prsir * gpay;
    return net_pay = gpay - (tax+prsi+dues);

}

//taxr = taxrate....prsir = prsirate....gpay = grosspay
4

3 回答 3

2

您的返回变量 net_pay 是您的函数的名称。只需这样做:

double net_pay (double hours, double gpay, double hrate , double dues , double taxr, double tax , double prsi , double prsir) {

    gpay = hours * hrate;
    tax = gpay * taxr;
    prsi = prsir * gpay;
    return gpay - (tax+prsi+dues);

}

并且它没有在你的 main 中声明:

int main()
{

string name;
const double hrate = 10.50;
const double taxr = .2;
const double prsir = .025;
const double dues = 3.50;
const int hours = 35;
cout << "enter name";
cin >> name;

double n_pay_var = net_pay ( hours,  gpay,  hrate, dues, taxr, tax, prsi, prsir);
cout << name;
cout << net_pay_var;


return 0;
}

您也没有在 main 中为 net_pay() 函数调用声明所有变量:gpay, tax and prsi.

所以 :

int main()
{

string name;
const double hrate = 10.50;
const double taxr = .2;
const double prsir = .025;
const double dues = 3.50;
const int hours = 35;
cout << "enter name";
cin >> name;


double tax ;//SET IT
double gpay;//SET IT
double prsi;//SET IT


cout << name;
cout << net_pay ( hours,  gpay,  hrate, dues, taxr, tax, prsi, prsir);


return 0;
}

double net_pay (double hours, double gpay, double hrate , double dues , double taxr, double tax , double prsi , double prsir) {

gpay = hours * hrate;
tax = gpay * taxr;
prsi = prsir * gpay;
return gpay - (tax+prsi+dues);

}
于 2013-04-19T13:44:09.183 回答
2

您正在使用未定义的参数调用该函数:

net_pay ( hours,  gpay,  hrate, dues, taxr, tax, prsi, prsir); // No no!
                  ^^^^                      ^^^  ^^^^

主要没有定义这些。尝试定义gpaytaxprsi在调用之前net_pay

 

返回值时,您不需要将其设置为该函数:

return net_pay = gpay - (tax+prsi+dues);  // No no!
       ^^^^^^^^^

只是

return gpay - (tax+prsi+dues);

 

此外,将结果存储net_pay在一个变量中:

double r = net_pay(hours,  gpay,  hrate, dues, taxr, tax, prsi, prsir);

cout << name;
cout << r;
cout << endl;
于 2013-04-19T13:44:48.323 回答
1

我认为这句话是错误发生的。

return net_pay = gpay - (tax+prsi+dues);

net_pay 是一个方法名而不是一个变量,所以像下面这样改变它:

return gpay - (tax+prsi+dues);
于 2013-04-19T13:45:29.123 回答