简体   繁体   中英

Errors in the code such as 'variable' was not declared in this scope

I need help, I'm trying to make a program that calculates the net pay of some person by taking in a few variables which in this case i have just made constants for speed.

These variables are: Name Mary, Hours worked (35), Hourly rate (10.50), Gross wage (367.50), Tax (@20%=73.50) ,PRSI (@2.5% =9.19) and Union dues (3.50).

These all get calculated to find the net pay.

I thought this would be easy but I can't understand where I went wrong, my code is:

#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

Your return variable net_pay is the name of your function. Simply do :

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

}

And its not declared in your 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;
}

You dont have all your variables declared in your main for the net_pay() function call too : gpay, tax and prsi.

So :

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

}

You're calling the function with arguments which are not defined:

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

Those are not defined in the main. Try to define gpay , tax and prsi before calling net_pay .

And when returning a value, you don't need set it to that function:

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

Just

return gpay - (tax+prsi+dues);

In addition, store the result of net_pay in a variable:

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

cout << name;
cout << r;
cout << endl;

I think this statement it's the error occurs.

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

the net_pay is a method name not a variable, so change it like bellow:

return gpay - (tax+prsi+dues);

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM