代码中的错误(例如“变量”)未在此范围内声明

时间:2013-04-19 13:38:41

标签: c++

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

这些变量是:姓名玛丽,工作小时数(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

3 个答案:

答案 0 :(得分: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);

}

它没有在你的主要声明:

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

}

答案 1 :(得分:2)

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

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

主要没有定义。在致电gpay之前,请尝试定义taxprsinet_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;

答案 2 :(得分:1)

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

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

net_pay是方法名称而不是变量,因此请将其更改为如下:

return gpay - (tax+prsi+dues);