我正在尝试从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对象的二维数组中。

这是我错误地尝试的操作:

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;
}

最佳答案

我想你想要更多这样的东西,

Payroll empData[numEmploy] ;

...

while ( count < numEmploy )
{
  inputFile >> empdata[count].payRate >> empdata[count].hoursWorked ;
  count ++ ;
}


那么您的工资就是empdata[x].payRate * empdata[x].hoursworked

如果阅读循环不在薪资的成员函数中,则必须使用设置器:

while ( count < numEmploy )
{
  double tmppay, tmphours ;
  inputFile >> tmppay >> tmphours ;
  empdata[count].setPayRate( tmppay ) ;
  empdata[count].setHoursWorked( tmphours) ;
  count ++ ;
}

10-04 15:11