我正在尝试从payroll.dat
输入此数据:
40.0 10.00
38.5 9.50
16.0 7.50
42.5 8.25
22.5 9.50
40.0 8.00
38.0 8.00
40.0 9.00
44.0 11.75
进入类payroll
个对象的2d数组。
以下是我错误尝试的内容:
int count = 0;
const int numEmploy = 7; // Number of employees
const int col = 2; // Number of categories
Payroll empData[numEmploy][col]; // Array to hold objects
ifstream inputFile;
inputFile.open("payroll.dat");
if (!inputFile)
cout << "There was an error opening the file. Please make sure it exists." << endl;
else
{
while(count < numEmploy && inputFile >> empData[][])
{
inputFile >> empData[count][0] >> empData[count][1];
count++;
}
}
最终,我需要将payroll.dat
中的值设为[0][0]
位置,然后乘以[0][1]
,然后乘以[1][0]*[1][1]
,再乘以[2][0]*[2][1]
等。将结果显示为总薪酬。
我认为我对>>
运算符的理解是关闭的。在这种情况下,这是流提取,按位或技术上两者?
我对正在发生的事情的叙述的理解是:只要count
小于numEmploy
并且empData[][]
从payroll.dat
接收值,请插入第一个可用的值将payroll.dat
中的数据块放入empData[0][0]
,将第二个块(第一个块右侧的块)插入empData[0][1]
。然后循环返回并将以下块(下一行,payroll.dat
的第一列)插入empData[1][0]
,然后将右边的块插入empData[1][1]
。继续此操作,直到count
大于或等于numEmploy
。然后每个empData[#][#]
将成为类Payroll
的对象。至少这是我想要的:P
甚至可以这样做吗?还是我坚持使用两个不同的阵列?
到目前为止,这是我的整个代码:
class Payroll
{
private:
double payRate; // holds an employee hourly pay rate
double hoursWorked; // an employee's hours worked
public:
Payroll() // empty constructor sets the payRate and hoursWorked to zero
{
payRate = hoursWorked = 0;
}
Payroll(double payR, double hoursW) //constructor checks for payR and hoursW to be positive
// and sets payRate and hours worked; sets to zero if negative values are provided
{
if (payR < 0) payR = 0;
if (hoursW < 0) hoursW = 0;
}
void setPayRate(double payR) //mutator for payRate; checks for payR to be positive or sets to zero
{
payRate = payR;
}
void setHoursWorked(double hoursW) //mutator for hoursWorked; checks for positive hoursW or sets to zero
{
hoursWorked = hoursW;
}
double getPayRate() //accessor to return payRate
{
return payRate;
}
double getHoursWorked() // accessor to return hoursWorked
{
return hoursWorked;
}
double getGrossPay() // computes and returns gross pay including OVERTIME, if any
{
float normHours, overHours, grossPay;
if (hoursWorked > 40)
{
overHours = (hoursWorked - 40);
normHours = (hoursWorked - overHours);
grossPay = (overHours * payRate * 1.5) + (normHours * payRate);
}
return grossPay;
}
};
int main ()
{
int count = 0;
const int numEmploy = 7; // Number of employees
const int col = 2; // Number of categories
Payroll empData[numEmploy][col]; // Array to hold objects
ifstream inputFile;
inputFile.open("payroll.dat");
if (!inputFile)
cout << "There was an error opening the file. Please make sure it exists." << endl;
else
{
while(count < numEmploy && inputFile >> empData[][])
{
inputFile >> empData[count][0] >> empData[count][1];
count++;
}
}
inputFile.close();
cout << fixed << showpoint << setprecision(2) << endl;
cout << "Employees' gross pay:" << endl;
for (int index = 0; index < numEmploy; index++)
{
empData[index][2].setPayRate();
empData[index][1].setHoursWorked();
cout << "Employee # " << index + 1 << empData.getGrossPay();
}
return 0;
}
答案 0 :(得分:0)
我想你想要更像这样的东西,
Payroll empData[numEmploy] ;
...
while ( count < numEmploy )
{
inputFile >> empdata[count].payRate >> empdata[count].hoursWorked ;
count ++ ;
}
然后你的工资只是empdata[x].payRate * empdata[x].hoursworked
如果读取循环不在Payroll的成员函数中,那么您必须使用setter:
while ( count < numEmploy )
{
double tmppay, tmphours ;
inputFile >> tmppay >> tmphours ;
empdata[count].setPayRate( tmppay ) ;
empdata[count].setHoursWorked( tmphours) ;
count ++ ;
}