简体   繁体   English

fstream数据中的列读取为零! (薪资对象数组)C ++

[英]Columns in fstream data reading zero! (Array of Payroll Objects) C++

I have been google searching like crazy and trying my best, but having some trouble. 我一直在谷歌搜索疯狂,尽我所能,但有一些麻烦。 Basically for homework, we gotta make an array of payroll objects, using the general format I've made below... 基本上,对于家庭作业,我们将使用我在下面编写的通用格式来制作薪水对象数组...

I'm having trouble. 我有麻烦了 The .dat file we are given to test looks like this: 提供给我们测试的.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

When I execute the program, it shows the first column of objects, but will always replace the 2nd column values with a 0. Any ideas? 当我执行程序时,它将显示对象的第一列,但始终将第二列的值替换为0。有什么想法吗?

(TL;DR.... Currently it reads "Employee #1: 40, 0" rather than "Employee #1: 40, 10.00"... etc) (TL; DR。...当前读为“ Employee#1:40,0”,而不是“ Employee#1:40,10.00” ...等)

Here's my code.... (not 100% done yet just testing datafile atm) 这是我的代码...(尚未完成100%的测试,只是测试数据文件atm)

#include <iostream>
#include <fstream>
using namespace std;

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 = 0;
            hoursWorked = 0;
        }
        Payroll(double payR, double hoursW) //constructor checks for payR and hoursW to be positive& sets to values
        {   if (payR < 0)
                payRate = 0;
            else 
                payRate = payR;

            if (hoursW < 0)
                hoursW = 0;
            else
                hoursW = hoursWorked;
        }
        void setPayRate(double payR) //mutator for payRate; checks for payR to be positive or sets to zero
        {   if (payR < 0)
                payRate = 0;
            else payRate = payR;
        }
        void setHoursWorked(double hoursW) //mutator for hoursWorked; checks for positive hoursW or sets to zero
        {
            if (hoursW < 0)
                hoursW = 0;
            else
                hoursW = hoursWorked;
        }
        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
        {   if (hoursWorked > 8)
                return ((payRate * 1.5) * hoursWorked);
            else 
                return (payRate * hoursWorked);
        }
};

int main()
{
    const int EMPLOYEES = 7;
    double payRate, hoursWorked;

    ifstream datafile;
    datafile.open("payroll.dat");

    Payroll payArray[EMPLOYEES];

    if (!datafile)
        cout << "AHH!! Error opening this file!! SRY!!!" << endl;
    else
    {   for (int index = 0; index < EMPLOYEES; index++)
        {   
            datafile >> payRate >> hoursWorked; 
            payArray[index].setPayRate(payRate);
            payArray[index].setHoursWorked(hoursWorked);
            cout << "Employee #" << index + 1 << ": ";
            cout <<  payArray[index].getPayRate() << ", " << payArray[index].getHoursWorked() << endl;

        }   
    }

}

Currently will output 目前将输出

Employee #1: 40, 0
Employee #2: 38.5, 0
Employee #3: 16.0, 0
...
Employee #7: 38, 0

I want it to read the other column without giving me zero :( Thank you, I am going to bed for now. 我想让它读另一列而不给我零:(谢谢,我现在要睡觉了。

The assignment statements in setHoursWorked are wrong; setHoursWorked中的赋值语句是错误的; they should be assigning to the instance variable hoursWorked , not the parameter hoursW . 它们应该分配实例变量hoursWorked ,而不是参数hoursW

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM