[英]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). 这些变量是:Mary姓名,工作时间(35),小时工资(10.50),工资总额(367.50),税金(@ 20%= 73.50),PRSI(@ 2.5%= 9.19)和工会会费(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. 返回变量net_pay是函数的名称。 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 : 并且它没有在您的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.
您也没有在net_pay()函数调用的主程序中声明所有变量:
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
. 尝试定义
gpay
, tax
以及prsi
之前调用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: 另外,将
net_pay
的结果存储在变量中:
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: net_pay是方法名而不是变量,因此请像下面这样更改它:
return gpay - (tax+prsi+dues);
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.