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;
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:
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.
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.
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.