Output not displaying correctly

I have read thru the archives, as well as my C++ book, and still cannot get my program to display properly. Can anyone point me is the right direction?

I have created two employee pointers:
1
2
Employee *employeeList1 = new Salaried(10000, 3);
Employee *employeeList2 = new Hourly(50, 40, "full time");


When I go to display them, employList1 displays the 1st parameter (10000) properly, however the second parameter displays -842150451 instead of 3.
For employeeList 2, the only parameter that displays properly is the 2nd one (40). The 1st one displays -62774385…… .00 instead of 50.00 and the 3rd (“full time”) displays the default of “unknown”.

For new Salaried object, it uses the construstor:
1
2
3
4
5
Salaried::Salaried(double salary, int managementLevel) : Employee(), minManagementLevel(0), maxManagementLevel(3), bonusPercent(.10)
{
 	managementLevel = 0;
	annualSalary = salary;
} 

The display code looks like this:
1
2
3
4
5
6
void Salaried::displayEmployee()
{
	Employee::displayEmployee();
	cout<<"Salaried Employee\n";
	cout<<"Management Level:\t" << managementLevel << "\n";
}

For the new Hourly object, it used the constructor:
1
2
3
4
5
6
7
Hourly::Hourly(double wage, double hours, string category) : Employee(), minWage(10), maxWage(75), minHours(0), maxHours(50)
{
	setWage(wage); 
	setHours(hours);
	setCategory(category);
	setAnnualSalary(calculatePay());
}

The display code looks like this:
1
2
3
4
5
6
7
8
void Hourly::displayEmployee()
{
	Employee::displayEmployee();
	cout<<"Hourly Employee\n"; 
	cout<<"Category:\t\t" << category << "\n";
	cout<<"Wage:\t\t\t" << wage << "\n";
	cout<<"Hours:\t\t\t" << hours << "\n";
}

1
2
3
4
5
Salaried::Salaried(double salary, int managementLevel) : Employee(), minManagementLevel(0), maxManagementLevel(3), bonusPercent(.10)
{
 	managementLevel = 0;
	annualSalary = salary;
}


Should be:
1
2
3
4
5
Salaried::Salaried(double salary, int managementlevel) : Employee(), minManagementLevel(0), maxManagementLevel(3), bonusPercent(.10)
{
 	managementLevel = managementlevel;
	annualSalary = salary;
} 


As for the call to salaried, you're having the same issue, the parameters should'nt be the same exact variable name as the ones used by your class. Meaning, the compiler has no idea which wage, hours, etc you're referring to
I thought about that and changed it, did not change the display so I put it back to 0.
Any other suggestions?? This has to be something small I an overlooking,
It's hard to tell since I don't know what all of your code is. If you can post all of it, I can look at it. If it's too big to post, please use http://www.pastebin.com
Volatile, is in the paste bin "Employee Class Project" Thanks!!
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
// Class members for Employee

        // Declare Data Members
        string firstName;
        string lastName;
        char gender;
        int dependents;
        double annualSalary;
        Benefit benefit;

// Class members for Salaried

        // Declare Data Members
        const int minManagementLevel;
        const int maxManagementLevel;
        const double bonusPercent;
        int managementLevel;

// Class members for Hourly

        // Declare Data Members
        const double minWage;
        const double maxWage;
        const double minHours;
        const double maxHours;
        double wage;
        double hours;
        string category;


Here are your function calls that need fixed:
1
2
3
4
5
6
7
8
9
10
// I suggest manLevel to match the variable in setManagementLevel
        Salaried(string firstName, string lastName, char gender, int dependents, double salary, Benefit ben, int managementLevel);
        Salaried(double salary, int managementLevel);

        Hourly(double wage, double hours, string category);
        Hourly(string firstName, string lastName, char gender, int dependents,
              double wage, double hours, Benefit benefit, string category);
        void setWage(double wage);
        void setHours(double hours);
        void setCategory(string category);


All of the variable names that are bolded conflict with member variables. This will cause problems. I didn't jump too far into your code simply because there is a lot of code to sift through. I'll take a better look while I wait for your reply.

Edit: I'll slowly pick your functions apart:
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
void Hourly::setCategory(string cat)
{
        // Did you mean if cat ==?
        if(category == "temporary" || category == "part time" || category == "full time")
                category = cat;
        else
                category = "Unknown";
}

// This needs fixed as well.
Salaried::Salaried(double salary, int managementLevel) : Employee(), minManagementLevel(0), maxManagementLevel(3), bonusPercent(.10)
{
        managementLevel = managementLevel;
        annualSalary = salary;
}

// This isn't correct
Salaried::Salaried(string firstName, string lastName, char gender, int dependents, double salary, Benefit ben, int managementLevel) :
        Employee (firstName, lastName, gender, dependents, salary, ben), minManagementLevel(0), maxManagementLevel(3), bonusPercent(.10)
{
        managementLevel = 0;
}

// Neither is this
void Employee::setDependents(int dep)
{
        if (dep >= minDependents && dep <= maxDepenents)
        {
                dependents = dep;
        }
        else if (dep < minDependents)
        {
                dep = minDependents;
        }
        else
        {
                dependents = maxDepenents;
        }
}


That's everything I could find. Give me a sample output of your program after you fix that stuff and I'll try to debug it further.
Last edited on
Volatile,

I did fine the issues in the setDepenedent function and the 2 Salaried constructors. Thank you for catching the confiling vairable names, believe it or not I looked at that many times, just didn't click for some reason. The only thing that still is an issue is that the wage is still not displaying correctly and I believe that is making the annual salary and weekly pay wig out. Am going to go back over the variables and functions and see what I missed and let you know. Thank you very much for the help and guidance, it is much appreciated.
I found a lot of places where wage is referred to with the same name as your member function. What I suggest doing is a find/replace with your IDE and search for wage and replace it with Wage. The places you want to change it are in the parameters for your member functions and within the actual code of the functions. You will need to evaluate each case and determine if it should be replaced. Aside from that, everything should be good.
Hey Volatile,

I have searched, replaces, checked and checked again. My brain is melting. Here are the 2 files that make up the last issue (wage not displaying), I am sure if it were a snake I would have been bit multiple times. I am going to step away for 5, do you see my error??

Hourly.h
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
// Preprocessor Directives
#pragma once
#include "employee.h"
//----------------------------------------------------------------------------------------------
// Define Hourly Class
class Hourly :
	public Employee
{
public:
	// Constructors and Deconstructor
	Hourly();
	Hourly(double wages, double hours, string category);
	Hourly(string firstName, string lastName, char gender, int dependents, double wages, double hours, Benefit benefit, string category);
	~Hourly();

	// Methods to Access Attributes
	double calculatePay();
	void displayEmployee();

	// Getters and Setters
	void setAnnualSalary(double salary);
	double getWage();
	void setWage(double wages);
	double getHours();
	void setHours(double hour);
	string getCategory();
	void setCategory(string category);

private:
	// Declare Data Members
	const double minWage;
	const double maxWage;
	const double minHours;
	const double maxHours;
	double wage;
	double hours;
	string category;	
};
//---------------------------------------------------------------------------------------------- 


Hourly.cpp

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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
// Preprocessor Directives
#include "stdafx.h"
#include "Hourly.h"
#include <iostream>
#include <iomanip>
//----------------------------------------------------------------------------------------------
// Declare Variables

const double minWage = 10;
const double maxWage = 75;
const double minHours = 0;
const double maxHours = 50;

// Default Hourly Constructor
Hourly::Hourly() : Employee(), minWage(10), maxWage(75), minHours(0), maxHours(50)
{
	wage = 0.0;
	hours = 0.0;
	category = "X";
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Hourly Constructor #1
Hourly::Hourly(double wages, double hours, string category) : Employee(), minWage(10), maxWage(75), minHours(0), maxHours(50)
{
	setWage(wages); 
	setHours(hours);
	setCategory(category);
	setAnnualSalary(calculatePay());
}
//----------------------------------------------------------------------------------------------
// Multi-Arg Hourly Constructor #2
Hourly::Hourly(string firstName, string lastName, char gender, int dependents, double wages, double hours, Benefit benefit, string category) :
	Employee(firstName, lastName, gender, dependents, 0, benefit), minWage(10), maxWage(75), minHours(0), maxHours(50)
{
	setWage(wages); 
	setHours(hours);
	setCategory(category);
	setAnnualSalary(calculatePay());
}
//----------------------------------------------------------------------------------------------
// Default Hourly Deconstructor
Hourly::~Hourly()
{
}
//----------------------------------------------------------------------------------------------
// CalculatePay Override Function
double Hourly::calculatePay()
{
	return wage * hours;
}
//----------------------------------------------------------------------------------------------
//Overloaded SetAnnualSalary
void Hourly::setAnnualSalary(double salary)
{
 	Employee::setAnnualSalary(calculatePay()*50);
}
//----------------------------------------------------------------------------------------------
// DisplayEmployee Override Function
void Hourly::displayEmployee(void)
{
	Employee::displayEmployee();
	cout<<"Hourly Employee\n"; 
	cout<<"Category:\t\t" << category << "\n";
	cout<<"Wage:\t\t\t" << getWage() << "\n";
	cout<<"Hours:\t\t\t" << hours << "\n";
}
//----------------------------------------------------------------------------------------------
// GetWage and SetWage Functions
double Hourly::getWage()
{
	return wage;
}
void Hourly::setWage(double wages)
{
	if(wages >= minWage && wages >= maxWage)
		wage = wages;
	else if (wages < minWage)
		wage = minWage;
	else if (wages > maxWage)
		wage = maxWage;
		

	setAnnualSalary(calculatePay());
}
//----------------------------------------------------------------------------------------------
//GetHours and SetHours Function
double Hourly::getHours()
{
	return hours;
}
void Hourly::setHours(double hour)
{
	if (hour >= minHours && hour <= maxHours)
		hours = hour;
	else if(hour < minHours)
		hours = minHours;
	else if(hour > maxHours)
		hours = maxHours;

	setAnnualSalary(calculatePay());
}
//----------------------------------------------------------------------------------------------
// GetCategory and SetCategory Functions
string Hourly::getCategory()
{
	return category;
}
void Hourly::setCategory(string cat)
{
	if(cat == "temporary" || cat == "part time" || cat == "full time") 
		category = cat;
	else
		category = "Unknown";
}
//---------------------------------------------------------------------------------------------- 

Last edited on
All I needed was the 5 mins away and came back and saw the mistake. Thanks for all of your help Volitale!!!
Topic archived. No new replies allowed.