如何获得所有五名员工的总薪酬总额?我已经尝试了一切,包括创建对象但似乎没有工作,我也必须将所有数据存储在一个名为EmpData的数组中,所以我不能改变它。我需要帮助。这是我创建的代码,到目前为止它运行正常。
#include<iostream>
using namespace std;
class Employee {
private:
double hourswrk;
double payrate;
double grosspay;
int empno;
char empname[20];
double netpay;
double tax;
double overt;
double overtime;
double taxdeduct;
public:
void getdetails();
void calculatepay();
void showdetails();
};
void Employee::getdetails()
{
cout << "\nEnter employee name:\n";
cin >> empname;
cout << "\nEnter employee number:\n";
cin >> empno;
cout << "Enter hours worked:";
cin >> hourswrk;
cout << "Enter rate of pay";
cin >> payrate;
}
void Employee::calculatepay()
{
tax = 0.25;
overt = 1.5;
if(hourswrk >= 60)
{
grosspay = 0;
netpay = 0;
taxdeduct = 0;
cout << "You have exceeded the amount of hours!";
}
else if(hourswrk <= 40)
{
grosspay = hourswrk * payrate;
}
else if(hourswrk > 40 && hourswrk < 60)
{
overtime = hourswrk - 40;
grosspay = overt * payrate*overtime + hourswrk * payrate;
}
taxdeduct = tax * grosspay;
netpay = grosspay - taxdeduct;
}
void Employee::showdetails()
{
cout << "Employee Payslip\n";
cout << "Name: " << empname;
cout << "Employee number:" << empno;
cout << "Basic Salary" << payrate;
cout << "Hours work" << hourswrk;
cout << "Grosspay" << grosspay;
cout << "Tax: " << taxdeduct;
cout << "Net Salary" << netpay;
cout << endl;
}
int main()
{
Employee EmpData[5];
int i;
double hourswrk;
double payrate;
double grosspay;
int empno;
char empname[20];
double netpay;
double tax = 0.25;
double taxdeduct;
double overt = 1.5;
double overtime;
for(int i = 0; i < 5; i++)
{
EmpData[i].getdetails();
EmpData[i].calculatepay();
EmpData[i].showdetails();
}
system("pause");
return 0;
}
答案 0 :(得分:1)
我刚刚添加了一个全局变量,每当您输入totgrosspay
grosspay
时grosspay
已添加到totgrosspay
#include<iostream>
long totgrosspay=0;
using namespace std;
class Employee {
private:
long grosspay=0;
double hourswrk;
double payrate;
int empno;
char empname[20];
double netpay;
double tax;
double overt;
double overtime;
double taxdeduct;
public:
void getdetails();
void calculatepay();
void showdetails();
};
void Employee::getdetails()
{
cout << "\nEnter employee name:\n";
cin >> empname;
cout << "\nEnter employee number:\n";
cin >> empno;
cout << "Enter hours worked:";
cin >> hourswrk;
cout << "Enter rate of pay";
cin >> payrate;
}
void Employee::calculatepay()
{
tax = 0.25;
overt = 1.5;
if(hourswrk >= 60)
{
grosspay = 0;
netpay = 0;
taxdeduct = 0;
cout << "You have exceeded the amount of hours!";
}
else if(hourswrk <= 40)
{
grosspay = hourswrk * payrate;
}
else if(hourswrk > 40 && hourswrk < 60)
{
overtime = hourswrk - 40;
grosspay = overt * payrate*overtime + hourswrk * payrate;
}
taxdeduct = tax * grosspay;
netpay = grosspay - taxdeduct;
totgrosspay= totgrosspay+grosspay;
}
void Employee::showdetails()
{
cout << "Employee Payslip\n";
cout << "Name: " << empname;
cout << "Employee number:" << empno;
cout << "Basic Salary" << payrate;
cout << "Hours work" << hourswrk;
cout << "Grosspay" << grosspay;
cout << "Tax: " << taxdeduct;
cout << "Net Salary" << netpay;
cout << endl;
}
int main()
{
Employee EmpData[5];
int i;
for(int i = 0; i < 5; i++)
{
EmpData[i].getdetails();
EmpData[i].calculatepay();
EmpData[i].showdetails();
}
cout<<totgrosspay;// it prints gross pay value
system("pause");
return 0;
}