Can't get info from a dat file

Ok, so I need to use an array and get the information from a .dat file. The dat file would have something like 40 15.5 45 16.75...
to make 7 rows and 2 columns, one is the hours worked and the other is the pay rate.

Here is my code. I can't get the file to populate the array. I get SUPER large numbers. So, I can't even try to multiply hours * rate in the end.

Please help. This has me very confused. I hope it's something really simple that I have overlooked.

If you see it, please point it out. Thank you to ALL that read this.



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

class Payroll
{
private:
double hours;
double payrate;
double gross;

public:
Payroll ()
{hours = 0.0;
payrate = 0.0;
gross = 0.0;}

Payroll (double hrs, double payrt)
{hours = hrs;
payrate = payrt;
gross = hours * payrate;}

double setHours (double hrs)
{hours = hrs;}

double setPayrate (double payrt)
{payrate = payrt;}

double getHours ()
{return hours;}

double getPayrate ()
{return payrate;}

double getGross ()
{return gross;}
};

int main ()
{
const int EmpHours = 7;
const int EmpRate = 2;
double pay[EmpHours][EmpRate];
double total = 0;
int hrs, rte;

Payroll totalPay[EmpHours][EmpRate];

ifstream datafile;
datafile.open("payroll.dat");
if (!datafile)
cout << "Error opening data file \n";
else
{for (hrs = 0; hrs < EmpHours; hrs++)
{for (rte = 0; rte < EmpRate; rte++)
{cout << "Column One" << (hrs + 1) << "Column Two" << (rte + 1);
datafile >> pay[hrs][rte];
cout <<pay[hrs][rte];}
cout <<endl;
datafile.close();
cout << endl << fixed << setprecision (2);
cout << "Employee #" << setw (2) << (hrs+1) << " your Gross amount is: $" << totalPay[EmpHours][EmpRate].getGross () << endl;
}
cout << endl << endl;
}

return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
#include <iostream>
#include <iomanip>
#include <fstream>
using namespace std;

class Payroll
{
private:
double hours;
double payrate;
double gross;

public:
Payroll ()
{hours = 0.0;
payrate = 0.0;
gross = 0.0;}

Payroll (double hrs, double payrt)
{hours = hrs;
payrate = payrt;
gross = hours * payrate;}

double setHours (double hrs)
{hours = hrs;}

double setPayrate (double payrt)
{payrate = payrt;}

double getHours ()
{return hours;}

double getPayrate ()
{return payrate;}

double getGross ()
{return gross;}
};

int main ()
{
const int EmpHours = 7;
const int EmpRate = 2;
double pay[EmpHours][EmpRate];
double total = 0;
int hrs, rte;

Payroll totalPay[EmpHours][EmpRate];

ifstream datafile;
datafile.open("payroll.dat");
if (!datafile)
cout << "Error opening data file \n";
else
{
   for (hrs = 0; hrs < EmpHours; hrs++)
   {
       for (rte = 0; rte < EmpRate; rte++)
       {
           cout << "Column One" << (hrs + 1) << "Column Two" << (rte + 1);
           datafile >> pay[hrs][rte];
           cout <<pay[hrs][rte];
       }
       cout <<endl;
       datafile.close(); /// don't close file here before you have read all lines
       cout << endl << fixed << setprecision (2);
       cout << "Employee #" << setw (2) << (hrs+1) << " your Gross amount is: $" << totalPay[EmpHours][EmpRate].getGross () << endl;
    }
    cout << endl << endl;
}

return 0;
} 
Last edited on
There's no need for a 2D array here. The data file has 7 rows and 2 columns, but each row holds the hours and rate for a single employee. All you need is a single-dimension array of Payroll objects.
Perhaps something along these lines:
1
2
3
4
5
6
7
8
9
    const int maxRows = 10;
    Payroll totalPay[maxRows];
    int rows = 0;
    double hours, rate;

    while ((rows<maxRows) && (datafile >> hours >> rate ))
    {
        totalPay[rows++] = Payroll(hours, rate);
    }


After that loop has completed, rows will contain the count of the number of rows of data which were read from the file. Then simply use a for loop to output the details for each employee held in the array.
Last edited on
Thanks vin, great catch. I still get garbage displayed from the dat file.

I'm trying Chervil's idea, but I only get one value returned from the dat file.

I'll keep trying.

Last edited on
That looks quite close. There are a couple of things wrong with this line:
1
2
cout << "Employee #" << setw (2) << (hours+1) 
    << " your Gross amount is: $" << totalPay[maxRows].getGross () << endl;

One problem - a significant one, is that totalPay[maxRows] is trying to access the 11th element of an array of 10 elements. Valid subscripts would be in the range 0 to maxRows-1.

The other problem is that you need to use a loop, to step through each row of the array. Because the count of the number of items read from the file is held in rows, the actual data is stored in totalPay[0] to totalPay[rows-1].

It should be pretty straightforward to add a for-loop to do that. Remember to change the value of employee number from (hours+1) so it changes each time around the loop.
THANKS CHERVIL.

As you were typing this reply, I was fixing it. I GOT IT!! WHEW!

Thank you for your AMAZING help!

My code became:

totalPay[rows++] = Payroll(hours, rate);
cout << fixed << setprecision (2);
cout << "Employee #" <<(rows) << " total hours are " << setw (2) << (hours) << " at a rate of $" << (rate) << "/hour. " << endl;
cout << "The Gross amount is: $" << (hours)*(rate) << endl << endl;
Well, that's sort of a solution, but it doesn't look right to me. I think you've missed the point somewhere.

Your cout statements are not making use of either the array, or of the Payroll object. Since you put the output statements in the loop where the data is read from the file, then there is no need for an array. And because the output is produced directly from the variables hours and rate then there is no need for the Payroll class either.

That makes about 40 lines of your code utterly redundant.

Remember the gross pay is calculated when the Payroll object is constructed. You should be making use of that.
Topic archived. No new replies allowed.