Need Help Finding Error in Employee Object/Class

I am creating a program that takes the user's information and displays it back on the screen. I have been successful in getting the majority of the program to work great. However, I am stuck on why the information isn't being processed by the class correctly. It seems to simply give me variable information that seems to be set to blank. The values output "-858993460". I am not very familiar with working with constructors, and I believe my problem is how the information is being sent to the class. If anyone can give me a good place to start, I would greatly appreciate your help.

Here is the main

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
  #include <iostream>
#include <iomanip>
#include <string>
#include "Employee.h"
#include "ProductionWorker.h"
using namespace std;
using std::string;
int main()
{
	string name;
	int number = 0;
	int date = 0;
	int empShift = 0;
	double empPayRate = 0.0;
	//Create a ProductionWorker object
	ProductionWorker stats;
	//Define info object and initalize it with values entered
	Employee info(name, number, date);

	//Get the employee's name
	cout << "Enter the employee's name:  ";
	getline(cin, name);
	//Get the employee's number
	cout << "Enter the employee's number:  ";
	cin >> number;
	//Get the date the employee was hired
	cout << "Enter the date the employee was hired:  ";
	cin >> date;

	//Get the employee's shift
	cout << "Enter the employee's shift where day shift = 1 and night shift = 2:  ";
	cin >> empShift;
	//Get the employee's pay rate
	cout << "Enter the employee's pay rate in decimal format (0.00):  ";
	cin >> empPayRate;
	//Store the employee's shift and pay rate into the stats object
	stats.setShift(empShift);
	stats.setPayRate(empPayRate);

	//Display the results
	cout << "\nThe employee's name is " << info.getEmployeeName() << endl;
	cout << "The employee's number is " << info.getEmployeeNumber() << endl;
	cout << "This employee was hired on " << info.getHireDate() << endl;
	cout << setprecision(3);
	cout << "This worker has the " << stats.getNameShift() << " shift and" << endl;
	cout << "makes $" << empPayRate << " per hour" << endl;

	cin.get();
	cin.get();
	return 0;
}



Here is the ProductionWorker Header file:

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

#ifndef PRODUCTIONWORKER_H
#define PRODUCTIONWORKER_H
#include "Employee.h"
#include <string>
class ProductionWorker : public Employee
{
private:
	int shift;
	double payRate;
public:
	//Default constructor
	ProductionWorker()
	{
		shift = 0;
		payRate = 0.0;
	}
	//Constructor
	ProductionWorker(double p)
	{
		payRate = p;
	}
	//Mutator functions
	void setShift(int s)
	{
		shift = s;
	}
	void setPayRate(double p)
	{
		payRate = p;
	}
	//Accessor functions
	string getNameShift();
};
#endif 


Here is the production worker .cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include "ProductionWorker.h"
#include <string>

string ProductionWorker::getNameShift()
{
	string nameShift;

	if (shift == 1)
		nameShift = "Day";
	if (shift == 2)
		nameShift = "Night";

	return nameShift;
}


Here is the Employee Header File:

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
#ifndef EMPLOYEE_H
#define EMPLOYEE_H
#include <string>
using std::string;
//Employee class declaration
class Employee
{
private:
	string employeeName;
	int employeeNumber;
	int hireDate;
public:
	//Default constructor
	Employee()
	{
		employeeName = ' ';
		employeeNumber = 0;
		hireDate = 0;
	}

	//Constructor
	Employee(std::string name, int number, int date)
	{
		set(name, number, date);
	}
	//Mutator functions
	void set(string, int, int);
	//Accessor functions
	string getEmployeeName() const
	{
		return employeeName;
	}
	int getEmployeeNumber() const
	{
		return employeeNumber;
	}
	int getHireDate() const
	{
		return hireDate;
	}
};
#endif 
Well. Right off the bat I can see what could be giving you some problems. I didn't run the code but you create Employee info(name, number, date). name hasn't been initialized to anything. number and date are both initialized to 0. So when you create the employee object. you are sending in parameters that have been initialized to 0 or nothing. Try creating the employee object after you have taken in the input from the user. I would start there.
Thank you for your quick response. I meant to respond sooner, but work has kept me very busy the past 2 days.

I was able to find my problem thanks to you. I had to pass the information like you mentioned and also set up proper mutator functions in my employee.h class. For some reason my hiredate still doesn't want to work, but i can live with that.

Thanks again!
Topic archived. No new replies allowed.