Hello,

I have an assignment for class that I have been working on and am nearly there. First of all, here is the description of the assignment:

"Employee and ProductionWorker Classes

Design a class named Employee. The class should keep the following information in member variables:

Employee name
Employee number
Hire Date

Write one or more constructors and the appropriate accessor and mutator functions for the class.

Next, write a class named ProductionWorker that is derived from the Employee class. The ProductionWorker class should have member variables to hold the following information:

Shift (an integer)
Hourly pay rate (a double)

The workday is divided into two shifts: day and night. The shift variable will hold an integer value representing the shift that the employee works. The day shift is shift 1 and the night shift is shift 2. Write one or more constructors and the appropriate accessor and mutator functions for the class. Demonstrate the classes by writing a program that uses a ProductionWorker object."

Phew! Sorry that was so long. So, I've written this program and it builds with no errors or warnings and when I run it, it seems to do everything it should except display the Employee's Name. All it displays for that is some weird symbol of something...

I will paste my code here and if anyone notices where I've messed up, could you please point me in the right direction? Thanks. Here's my code:

//This program demostrates a class and a derived class with constructors.

#include <iostream>
#include <string>
#include <iomanip>
using namespace std;

class Employee
{
	private:
		char EmpName;
		int EmpNum;
		int HireDate;

	public:
		void setEmpName(char);
		void setEmpNum(int);
		void setHireDate(int);
		char getEmpName() const;
		int getEmpNum() const;
		int getHireDate() const;
		Employee();
};

void Employee::setEmpName(char x)
{
	EmpName = x;
}

void Employee::setEmpNum(int y)
{
	EmpNum = y;
}

void Employee::setHireDate(int z)
{
	HireDate = z;
}

char Employee::getEmpName() const
{
	return EmpName;
}

int Employee::getEmpNum() const
{
	return EmpNum;
}

int Employee::getHireDate() const
{
	return HireDate;
}

Employee::Employee()
{
	cout << "I will ask you some questions about an employee.\n\n";
}

class ProductionWorker : public Employee
{
	private:
		int Shift;
		double HourlyPayRate;

	public:
		void setShift(int);
		void setHourlyPayRate(double);
		int getShift() const;
		double getHourlyPayRate() const;
		ProductionWorker();
};

void ProductionWorker::setShift(int a)
{
	Shift = a;
}

void ProductionWorker::setHourlyPayRate(double b)
{
	HourlyPayRate = b;
}

int ProductionWorker::getShift() const
{
	return Shift;
}

double ProductionWorker::getHourlyPayRate() const
{
	return HourlyPayRate;
}

ProductionWorker::ProductionWorker()
{
	cout << "After answering the questions,\n";
	cout << "I will display the employee's information.\n\n\n";
}

int main()
{
	ProductionWorker info;
	char name[100];
	int num;
	int date;
	int shift;
	double rate;

	cout << "What is the employee's name? ";
	cin.getline(name, 100);
	
	cout << "What is the employee's number? ";
	cin >> num;
	
	cout << "What is the employee's hire date?\n";
	cout << "(Month, day, and year without any slashes,\n";
	cout << "dashes, commas, or other punctuation.)\n";
	cout << "For example, January 14, 1983 would look like 01141983. ";
	cin >> date;
	
	cout << "Does the employee work shift 1 or shift 2? ";
	cin >> shift;
	
	cout << "How much does the employee make per hour? ";
	cin >> rate;


	info.setEmpName(name[100]);
	info.setEmpNum(num);
	info.setHireDate(date);
	info.setShift(shift);
	info.setHourlyPayRate(rate);

	cout << "\n\nHere is the employee's data:\n\n";
	cout << "Employee's Name: " << info.getEmpName() << endl;
	cout << "Employee's Number: " << info.getEmpNum() << endl;
	cout << "Employee's Hire Date: " << info.getHireDate() << endl;
	cout << "Employee's Shift: " << info.getShift() << endl;
	cout << setprecision(2) << fixed;
	cout << "Employee's Hourly Pay Rate: $" << info.getHourlyPayRate() << endl << endl;

	return 0;
}

PS: I'm still new to these forums and last time I made a post, I was called out on not using the code tags. I tried to make sure I used them this time. I hope I did it correctly.

char EmpName; Char holds exactly that: 1 char. Make it into a string.

Edited 6 Years Ago by jonsca: n/a

char EmpName; Char holds exactly that: 1 char. Make it into a string.

Thank you!!! I don't know why I was trying to use a char array. Using a string is so much easier. Thanks again! Now my program works perfectly!!!

This question has already been answered. Start a new discussion instead.