Class and array program

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 . . .
Payday[WORKER] is out of bounds.
I'm sorry Peter87, I have been looking at the code and I don't understand, were Payday [WORKER] is out of bounds or what does that means ?
Line 10: WORKER has a value of 7.

Line 13: You allocate 7 instances of Payday. These instances are [0]-[6]. Subscripts in C++ start at 0. Payday[7] is not a valid reference.

line 39 - you may have intended to use Payday[count] instead of Payday[WORKER]
The variable count is not initialized.
iQChange wrote:
The variable count is not initialized.

But it is assigned to before usage so it's not a problem, but you have a point. Why declare the count variable outside the loop. Better declare it inside the for statement so that it can't be used accidentally outside the loop.

1
2
int count;
for(int count = 0; count < WORKER; ++count)

Last edited on
Also some of your functions have no use. I re-made your program.
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
#include <iostream>

class Worker
{
    private:
    int worked_hours_;
    double pay_rate_;
    double payment_;

    public:
    Worker() : worked_hours_(0), pay_rate_(0), payment_(0) {}
    bool set_payrate(double pay_rate)
    {
        if(pay_rate > 0)
        {
            pay_rate_ = pay_rate;
            if(worked_hours_ > 0)
            {
                payment_ = pay_rate_ * worked_hours_;
            }
            return true;
        }
        else return false;
    }

    bool set_hours(int hours)
    {
        if((hours < 0) || (hours > 60) ) return false;
        else
        {
            worked_hours_ = hours;
            if(pay_rate_ > 0)
            {
                payment_ = pay_rate_ * worked_hours_;
            }
            return true;
        }
    }
    double payment() {return payment_;};
    int worked_hours() {return worked_hours_;}
    double pay_rate() {return pay_rate_;}
};

int main(int, char*[])
{
    Worker workers[7];
    for(auto &worker : workers)
    {
        double pay_rate = 11.11;
        int worked_hours = 0;

        //Yeah, I think that's a bad pratice, but we're in ranged-for loop. What can I do? (Don't answer :P)
        while(true)
        {
            if(!worker.set_payrate(pay_rate)) return -1;
            std::cout << "\nInsert worker's worked hours: ";
            std::cin >> worked_hours;
            if(!worked_hours || !worker.set_hours(worked_hours))
            {
                std::cout << "\nInsert a valid number of hours.\n";
                continue;
            }
            else break;
        }
    }
    for(auto worker : workers)
        worker.payment() > 0? std::cout << "\nWorker payment: " << worker.payment() : std::cout << "\nSomething went wrong...";
}


This works perfect. Oh, and I remade YOUR code:
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
74
75
76
77
78
79
#include <iostream>
class Payroll
{
private:
	double rate, hours, tpay;

public:
	Payroll();

	void setRate(double);
	void setHours(double);
	void setTpay(double);

	double getRate();
	double getHours();
	double getTpay();
};
// 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; }
using namespace std;
void displaypayroll( Payroll * const);
int main()
{
	const int WORKER = 7;
	// Array of 7 worker's
	Payroll Payday[WORKER];

	// Get worker hours
	for(int count = 0; count < WORKER; ++count)
	{
	    double Prate, Hwork, Totalp;
		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(int count = 0; count < WORKER; ++count)
	{
		displaypayroll( & Payday[count]);
	}
	return 0;
}
void displaypayroll(Payroll * const x)
{

	cout << "Gross pay: " << x->getTpay() << endl;
}
Sorry for the late reply, just wanted to thank all of you for your help. :D
Topic archived. No new replies allowed.