gpt4 book ai didi

c++ - 'variable' was not declared in this scope 等代码中的错误

转载 作者:太空宇宙 更新时间:2023-11-04 16:21:36 25 4
gpt4 key购买 nike

我需要帮助,我正在尝试编写一个程序,通过输入一些变量来计算某人的净工资,在这种情况下,我只是为速度设置了常量。

这些变量是:姓名 Mary、工作小时数 (35)、时薪 (10.50)、总工资 (367.50)、税收 (@20%=73.50)、PRSI (@2.5% =9.19) 和 union 会费 (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

最佳答案

您的返回变量 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;
}

您也没有在主函数中为 net_pay() 函数调用声明所有变量:gpay、tax 和 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);

}

关于c++ - 'variable' was not declared in this scope 等代码中的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16105994/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com