我目前正在上C ++课,需要帮助我的代码的某一部分。这一切都编译但是当我去测试它时,我只允许在其余代码没有输入的情况下向窗口输入一个值。
作为参考,这是我要回答的问题: 11.薪资
设计一个PayRoll类,其中包含员工每小时工资率的数据成员,double类型的工作小时数。默认构造函数将工作小时数和支付率设置为零。该类必须具有mutator功能,以设置每个员工的工资率和工作小时数。该课程应包括工作小时数和工资率的访问者。该类最后应具有getGross函数,该函数将返回通过乘以工作小时数计算的double 薪水。
编写一个包含七个PayRoll对象数组的程序。该计划应询问用户每位员工的工资率以及每位员工的工作小时数。一定要包括一名声称每周工作超过60小时的员工。打印出所有员工各自在自己行上的员工阵列号,工作时数,工资率和总工资。设置打印双精度到两位小数的精度。
输入验证:不要接受大于60的工作小时数,只需将设定的功能设定工作小时数设为60,即允许的最大值。
我的代码如下:
#include <iostream>
#include <iomanip>
//Garrett Bartholomay
/*This program defines and implements the Payroll class.
* The class is then used in a program that calculates gross pay for an array of
* Payroll objects after accepting values for hours and pay rate from standard input*/
class Payroll
{
private:
int hoursWorked;
double payRate;
public:
Payroll();
Payroll(int, double);
void setHours(int);
void setPayRate(double);
int getHours() const;
double getPayRate() const;
double getGross()const;
};
Payroll::Payroll()
{
hoursWorked = 0;
payRate = 0.0;
}
Payroll::Payroll(int h, double r)
{
payRate = r;
hoursWorked = h;
}
void Payroll::setHours(int h)
{
hoursWorked = h;
}
void Payroll::setPayRate(double p)
{
payRate = p;
}
int Payroll::getHours() const
{
return hoursWorked;
}
double Payroll::getPayRate() const
{
return payRate;
}
double Payroll::getGross() const
{
double gross = static_cast<double>(hoursWorked) * payRate;
return gross;
}
using namespace std;
int main()
{
const int NUM_EMPLOYEES = 7;
Payroll employee[NUM_EMPLOYEES];
int pay;
int hours;
int i;
double grossPay;
for (i = 0; i < NUM_EMPLOYEES; i++)
{
cout << "Enter the # " << (i) << " employee's rate of pay per hour: ";
cin >> pay;
cout << "Enter the # " << (i) << " employee's hours worked for the week: ";
cin >> hours;
employee[i].setPayRate(pay);
employee[i].setHours(hours);
}
cout << "\n\nHere is the gross pay for each employee:\n";
cout << fixed << showpoint << setprecision(2);
for (int i = 0; i < NUM_EMPLOYEES; i++)
{
grossPay = employee[i].getGross();
cout << "The gross pay for employee # " << (i) << " is: " << grossPay << endl;
}
return 0;
}
输入位于循环内。
非常感谢任何帮助。
谢谢,