I am a newbie to C++ . i am trying to , i think, answer this question and i am lost many hours wasted. I think I've broken the problem done correctly.
(NOTE WE CANT USE VECTORS OR STRUCTURE (still newbie) ONLY = CLASS< FUNCTIONS ARRAYS)
1)i need to design a pay class that has data members for an employee’s hourly pay rate and number of hours worked.
2)Write a program with an array of seven pay objects.
3) a.The program should read the number of hours each employee worked b.and The program should read their hourly pay rate from a file
4) call class functions to store this information in the appropriate objects.
5) It should then call a class function, a. once for each object, b. to return the employees gross pay,
6) so this information can be displayed.
Issues: I can read data from the file ( it has to columns) but i cant read it into an object. so the object functions can store it to calculate the gross
// here what I have can anyone help me , i know this is probably simple but I'm complete confused and i have to understand arrays and classes before I move on.
#include <iostream>
#include <fstream>
#include <iomanip>
using namespace std;
Class Payroll
{
private:
int hours;
double rate, gross;
public:
Payroll() // default constructor
{
hours = rate = 0;
}
// Set
void setHours(int h)
{ hours = h; }
void setRate(double r)
{ rate = r; }
// Get
double getGross (int hours, double rate r])
{
gross = hours * rate;
return gross;
}
};
// trying write a function that take array display results
void showfunction(int array[],int); // lost here too!
int main ()
{
Payroll sevenObjects[7];
const int EMP_RATE = 7 ;// sets number of Employees
const int EMP_HOURS = 7;
int hours [EMP_HOURS];
double rate [EMP_RATE];
int count ;
ifstream datafile; // used to read data
datafile.open ("payroll.dat"); // Open file
if( !datafile)
cout << "error opening data file \n";
else
{
/* i tried nested and getline, probably wrong. i tried 2D Array[][] but didnt know how to pass to member function */
for (count = 0 ; count < lines; count++)
{
>> hours[count]>>rate[count];
cout << hours[count]<<" " <<rate[count]<< endl;
}
}
datafile.close();
return 0;
}
payroll
40.0 10.00
38.5 9.50
16.0 7.50
22.5 9.50
40.0 8.00
38.0 8.00
40.0 9.00