The issue that I'm having is with the output, ex: 1.65516e-313 instead of the calculated gross pay amount. What is wrong ? Any assistance will be appreciated.
This is my assignment:
Write a class name Payroll, with the class declaration in a file called Payroll.h and the implementation in a file called Payroll.cpp. The class will have data members for an employee's hourly pay rate, number of hours worked and total pay for the week. All of the data members will be doubles. The class only needs one constructor which can be a default constructor that sets all data members to zero. Then add the mutators and accessor for the class. The program will have an array of 7 Payroll objects. The program will prompt the user for number of hours each employee has worked and will then display the amount of gross pay each has earned. Before asking the user for the hours, the program should set the pay rate for each employee without user input.
Validation:
Do not accept values greater than 60 for the number of hours worked.
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
|
// This is Payroll main
//
#include <iostream>
#include "Payroll.h"
using namespace std;
void displaypayroll( Payroll * const);
int main()
{
double Prate, Hwork, Totalp;
const int WORKER = 7;
int count;
// Array of 7 worker's
Payroll Payday[WORKER];
// Get worker hours
for(count = 0; count < WORKER; ++count)
{
Prate = 11.11;
cout << "Enter the number of hours worked: " << endl;
cin >> Hwork;
if (Hwork < 0)
{
cout <<"NO NEGATIVE HOURS ARE ALLOWED\n ";
count--;
}
if (Hwork > 60)
{
cout <<"Valid hours are from 0 - 60\n ";
count--;
}
Totalp = Prate * Hwork;
Payday[count].setRate(Prate);
Payday[count].setHours(Hwork);
Payday[count].setTpay(Totalp);
}
for(count = 0; count < WORKER; ++count)
{
displaypayroll( & Payday[WORKER]);
}
return 0;
}
void displaypayroll(Payroll * const x)
{
cout << "Gross pay: " << x->getTpay() << endl;
}
|
Here is the header file.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
class Payroll
{
private:
double rate, hours, tpay;
public:
Payroll();
void setRate(double);
void setHours(double);
void setTpay(double);
double getRate();
double getHours();
double getTpay();
};
|
Here is the implementation.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23
|
#include "Payroll.h"
// Constructor
Payroll::Payroll ()
{
rate = 0;
hours = 0;
tpay = 0;
}
// Mutator member functions
void Payroll::setRate(double r) { rate = r; }
void Payroll::setHours(double h) { hours = h; }
void Payroll::setTpay(double t) { tpay = t; }
// Accessor member funtions that returns values
double Payroll::getRate() { return rate; }
double Payroll::getHours() { return hours; }
double Payroll::getTpay() { return tpay; }
|
Program output:
Enter the number of hours worked:
10
Enter the number of hours worked:
20
Enter the number of hours worked:
30
Enter the number of hours worked:
40
Enter the number of hours worked:
50
Enter the number of hours worked:
60
Enter the number of hours worked:
5
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Gross pay: 1.65516e-313
Press any key to continue . . .