I have been working on this project for a while and need help understanding why I get the results I do.
First the display of the after compiling the code:

Enter the employee's name: Bob Green
Enter the employee's number: 112
Enter the date the employee was hired: 01091989
Enter the employee's shift where day shift = 1 and night shift = 2: 2
Enter the employee's pay rate in decimal format (0.00): 23.00

The employee's name is
The employee's number is -858993460
This employee was hired on -858993460
This worker has the Night shift and
makes $23 per hour
Press any key to continue . . .
Now the code I am using to get this result
Production h.

#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

Employee.h

#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

Main.cpp

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

Production.cpp

#include "ProductionWorker.h"
#include <string>
 
string ProductionWorker::getNameShift()
{
	string nameShift;
 
	if (shift == 1)
		nameShift = "Day";
	if (shift == 2)
		nameShift = "Night";
 
	return nameShift;
}

Empployee.cpp

#include "Employee.h"
#include <string>
 
void Employee::set(string name, int number, int date)
{
 
}

I am new to the C++ community so please help me understand why this is giving me the results I am getting.

Recommended Answers

All 6 Replies

Welcome the DaniWeb :)


I had this problem a few days ago. I always thought that you could define a variable like this:

int a;

but it always picks up a weird value and not zero like I would have thought. Make sure that all your variable definitions are defined to (eg.):

int a = 0;
float b = 0.0;
string c = "";

etc.

If this helps, upvote the post. Thanks :)

Thanks for the welcome.

I have already defined the variables. I am just not understanding why it will not display anything at all after putting in the name of the employee and -858993460 for both the hire date and employee number. I am sure it is something simple I just can not seem to find it.

When you define a variable, you need to give it a value. Change these lines to what I write:

In production.h
Lines 9 + 10

int shift=0;
double payRate=0;

In main.cpp
Lines 12 + 13

int number=0;
int date=0;

Now I am getting the following:
>------ Build started: Project: Project7, Configuration: Debug Win32 ------
1>Compiling...
1>Main.cpp
1>c:\documents and settings\greg\my documents\visual studio 2008\projects\project7\project7\productionworker.h(9) : error C2864: 'ProductionWorker::shift' : only static const integral data members can be initialized within a class
1>c:\documents and settings\greg\my documents\visual studio 2008\projects\project7\project7\productionworker.h(10) : error C2864: 'ProductionWorker::payRate' : only static const integral data members can be initialized within a class
1>Generating Code...
1>Compiling...
1>production.cpp
1>c:\documents and settings\greg\my documents\visual studio 2008\projects\project7\project7\productionworker.h(9) : error C2864: 'ProductionWorker::shift' : only static const integral data members can be initialized within a class
1>c:\documents and settings\greg\my documents\visual studio 2008\projects\project7\project7\productionworker.h(10) : error C2864: 'ProductionWorker::payRate' : only static const integral data members can be initialized within a class
1>Generating Code...
1>Build log was saved at "file://c:\Documents and Settings\GREG\My Documents\Visual Studio 2008\Projects\Project7\Project7\Debug\BuildLog.htm"
1>Project7 - 4 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========

Can anyone help with this problem I just can not seem to get this to display correctly? I just want to know why I get that when I compile it and enter the information about the employee.

It is a bit more complex than that. You have made a whole set of basic errors, and I think you have to fix all of them to get yourself out of jail here.

Let me start: First off you have TWO constructors, (a) the default constructor and (b) a valued constructor. The first is ok, [but ugly] and the second is junk.

The second is written like this:

Employee(std::string name, int number, int date)
    { set(name, number, date); 

void 
Employee::set(string name, int number, int date)
{ 
}

So obviously nothing gets set.

Next YOU DO NOT HAVE A copy constructor OR an assignment operator. If you have a non-POD [Plain Old Data] class, and in this case std::string is NOT a plain data object, ALWAYS write those two methods, unless you are into that super guru c++ class, which I can only aspire to.

This does not actually affect you here BUT it will as soon as you do anything else.

If you fix set, then this program works.

However, I don't understand why you get a ProdcutionWorker, and then create an Employee. I think that you wanted a production worker. so why not do a

// Remove this: 
// Employee info(name,number,date);
// and change it to this:
stats.set(name,number,date);
// change all instances of info to stats in the output.
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.