Hello once again. I'm trying to do one last project, but unlike the others, that usually i struggle with some common errors, this time i have problems figuring out how its supposed to be created.

This is the question:

Write a class named Employee that has the following member variables:
name, idnumber, department, position

and have the following constructors.
-one that accepts values as arguments and assigns them to the appropiate member variables, employee name, employee id, department, position
-accepts the following values as arguments and assigns them to the appropiate member variables name, id. the department and position fields should be assigned and empty string ("")
-a default constructor that assigns empty strings ("") to the name, department, and position member variables and 0 to the idnumber member variable.

Write appropriate mutator functions that store values in these member variables. once you have written the class, write a separete program that creates three employee objects to hold the data :
" susan meyers, 47899, accounting, vice president"
"Mark jones, 39119, IT, programmer"
"Joy Rogers, 81774, manufacturing, Engineer"

The program should store this data in the three objects and then display the data for eac h employee on the screen

This is what i think could be included in the first program:

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

class Employee
{
	private:
		string Name;
		int IdNumber;
		string Department;
		string Position;
	public:
		Employee(string AssignName,int AssignIdNumber,string AssignDepartment,string AssignPosition)                       
		{
			Name=AssignName;
			IdNumber=AssignIdNumber;
			Department=AssignDepartment;
			Position=AssignPosition;
		}
		Employee(string AssignName,int AssignIdNumber)
		{
			Name=AssignName;
			IdNumber= AssignIdNumber;
			Department="";
			Position="";
		}
		Employee()
		{
			Name="";
			IdNumber=0;
			Department="";
			Position="";
		}
		
		string getName() const;
		int getIdNumber() const;
		string getDepartment() const;
		string getPosition () const;
};

string Employee::getName() const
{
	return Name;
}
int Employee::getIdNumber() const
{
	return IdNumber;
}
string Employee::getDepartment() const
{
	return Department;
}
string Employee::getPosition() const
{
	return Position;
}

The second part, the second program i think holds something like:

#include <iostream>
#include "EmployeeClass"
using namespace std;

int main()
{
	
	
	Employee Name[3] ={ "Susan Meyer", "Mark Jones", "Joy Rogers"};
	Employee Idnumber[3] ={ 47899, 39119,81774};
	Employee Department[3] = { "Accounting", "IT", "Manufacturing"};
	Employee Position[3] = {"Vice President", "Programmer", "Engineer"};

	return 0;
	
}

As to how to have two separete programs combined to make one ???never done anything as advanced as this, PLEASE HELP.

Recommended Answers

All 18 Replies

Well, You have misunderstood the basic concept of a class

Employee Name[3] ={ "Susan Meyer", "Mark Jones", "Joy Rogers"};
	Employee Idnumber[3] ={ 47899, 39119,81774};
	Employee Department[3] = { "Accounting", "IT", "Manufacturing"};
	Employee Position[3] = {"Vice President", "Programmer", "Engineer"};

So by this code you are actually creating about 12 Employee data types.

Where as you only need to make 3 .......

Just getback to http://cplus.about.com/od/learning1/ss/cppobjects.htm
This small tutorial on classes and i think you will get to know what exactly is going wrong in your code.

commented: Explains what he sees wrong, really good. +1

Let me know if im starting to turn towards the right direction. I think this is a good way to just have 3 employee data types. Let me know if i did this correct and how to link both programs. Thanks so much.

#include <iostream>
#include "EmployeeClass"
using namespace std;
int main()
{
	const int NUM_WORKERS = 3;
	employeedata EMPLOYEE[NUM_WORKERS] ={
		employeedata("Susan Meyers", 47899, "Accounting", "Vice President"),
		employeedata("Mark Jones", 39119, "IT", "Programmer"),
		employeedata("Joy Rogers", 81774, "Manufacturing", "Engineer")};

		cout << " NAME	ID NUMBER      	DEPARTMENT	POSITION\n";
		cout << "-------------------------------------------\n";

		for (int i = 0; i < NUM_WORKERS; i++)
		{
			cout << setw(8) << EMPLOYEE[i].getName();
			cout << setw(8) << EMPLOYEE[i].getIdnumber();
			cout << setw(8) << EMPLOYEE[i].getDepartment();
			cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
		}
		return 0;
}

Are you getting an error?

What is the name of the file in which the class declaration is stored?

I finally got the file to open i think i no longer get that error now these are the errors i get:
error C2065: 'employeedata' : undeclared identifier
error C2146: syntax error : missing ';' before identifier 'EMPLOYEE'
error C2065: 'EMPLOYEE' : undeclared identifier
error C2059: syntax error : '{'
error C2143: syntax error : missing ';' before '{'
error C2143: syntax error : missing ';' before '}'
error C3861: 'employeedata': identifier not found
error C3861: 'employeedata': identifier not found
error C3861: 'employeedata': identifier not found
error C2065: 'EMPLOYEE' : undeclared identifier
C2228: left of '.getName' must have class/struct/union
error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getIdnumber' must have class/struct/union error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getDepartment' must have class/struct/union
error C2065: 'EMPLOYEE' : undeclared identifier
error C2228: left of '.getPosition' must have class/struct/union


This program is called Data Two:

#include <iostream>
#include <iomanip>
#include "Employee Class.cpp"
using namespace std;

int main()
{
	const int NUM_WORKERS = 3;
	employeedata EMPLOYEE[NUM_WORKERS] ={
		employeedata("Susan Meyers", 47899, "Accounting", "Vice President"),
		employeedata("Mark Jones", 39119, "IT", "Programmer"),
		employeedata("Joy Rogers", 81774, "Manufacturing", "Engineer")};

		cout << " NAME	ID NUMBER	DEPARTMENT	POSITION\n";
		cout << "-------------------------------------------\n";

		for (int i = 0; i < NUM_WORKERS; i++)
		{
			cout << setw(8) << EMPLOYEE[i].getName();
			cout << setw(8) << EMPLOYEE[i].getIdnumber();
			cout << setw(8) << EMPLOYEE[i].getDepartment();
			cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
		}
		return 0;
}

This is the Other file and what i have changed it those far. This one is called Employee Class

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

class Employee
{
	private:
		string Name;
		int IdNumber;
		string Department;
		string Position;
	public:
		Employee(string AssignName,int AssignIdNumber,string AssignDepartment,string AssignPosition)                       
		{
			Name=AssignName;
			IdNumber=AssignIdNumber;
			Department=AssignDepartment;
			Position=AssignPosition;
		}
		Employee(string AssignName,int AssignIdNumber)
		{
			Name=AssignName;
			IdNumber= AssignIdNumber;
			Department="";
			Position="";
		}
		Employee()
		{
			Name="";
			IdNumber=0;
			Department="";
			Position="";
		}
		void setName(string);
		void setIdNumber(int);
		void setDepartment(string);
		void setPosition(string);
		string getName() const;
		int getIdNumber() const;
		string getDepartment() const;
		string getPosition () const;
};

void Employee::setName(string NA)
{
	Name = NA;
}

void Employee::setIdNumber(int ID)
{
	IdNumber = ID;
}
void Employee::setDepartment(string DEP)
{
	Department = DEP;
}
void Employee::setPosition (string POS)
{
	Position = POS;
}

string Employee::getName() const
{
	return Name;
}
int Employee::getIdNumber() const
{
	return IdNumber;
}
string Employee::getDepartment() const
{
	return Department;
}
string Employee::getPosition() const
{
	return Position;
}

Hopefully this can give you guys some info as to how clustered my mind is right now. That is why i need the help BAD. Thanks.

#include "Employee Class.cpp"

There should be no space between the name , it should be

#include "EmployeeClass.cpp"

commented: Gives good advice +1

If i take out the space then it won't open the file, I have to leave the space, because that is how i named it. And that part works, but after it opens it is when i get those tons of errors.

Could some one give some more pointers as to how to do this. Thanks

Fields must be initialized with constructor.

int main()
{
   Employee a[3]={Employee("A",1,"A","A"),Employee("B",2,"B","B"),Employee("C",3,"C","C")};
   for(int i=0;i<3;i++)
   {
    cout << "\n" << a[i].getName();
   }
	return 0;
}

I have been working on this all night, i'm like right there in getting it to work, could you guys tell me what these errors mean, and how to fix them so that i can get this program working. Thanks you.


This is what i have for the file employee class:

#ifndef EmployeeClass
#define EmployeeClass
#include < cstring>

const int DEFAULT_SIZE = 51;

class EmployeeData
{
private:
	char *name;
	int idnumber;
	char department;
	char position;

	//private member function
	void createName(int size, char *value)
	{//Allocate the default amount of memory for name
		name = new char [size];

		//store a value in the memory
		strcpy(name, value);}

public:
	//Constructor #1
	EmployeeData()
	{//Store an empty string in the name attribute.
		createName(DEFAULT_SIZE, "");

		//initialize idnumber department position
		idnumber = 0;
		department = 0;
		position = 0;}

	//Constructor #2
	EmployeeData(char *nam)
	{//allocate memory and store the description.
		createName(strlen(nam), nam);

		//initialize idnumber department position
		idnumber = 0;
		department = 0;
		position = 0;}
	
	//Constructor #3
	EmployeeData(char *nam, int id, char d, char p)
	{//Allocate memory and store the description.
		createName(strlen(nam), nam);

		//Assign values to id deparment position
		idnumber = id;
		department = d;
		position = p;}

	//Destructor
	~EmployeeData()
	{ delete[] name;}

	//mutator functions
	void setName(char *n)
	{strcpy(name, n);}

	void setIdnumber(int id)
	{idnumber = id;}

	void setDepartment(char d)
	{department = d;}

	void setPosition(char p)
	{position =p;}

	//Accessor functions
	const char *getName() const
	{return name;}

	int getIdnumber() const
	{return idnumber;}

	char getDepartment() const
	{return department;}

	char getPosition() const
	{return position;}
};
#endif

This is what i have for the file Data Two:

#include <iostream>
#include <iomanip>
#include "Employee Class.cpp"
using namespace std;

int main()
{
	const int NUM_WORKERS = 3;
	EmployeeData EMPLOYEE[NUM_WORKERS] ={
		EmployeeData("Susan Meyers", 47899, "Accounting", "Vice President"),
		EmployeeData("Mark Jones", 39119, "IT", "Programmer"),
		EmployeeData("Joy Rogers", 81774, "Manufacturing", "Engineer")};

		cout << " NAME	ID NUMBER	DEPARTMENT	POSITION\n";
		cout << "-------------------------------------------\n";

		for (int i = 0; i < NUM_WORKERS; i++)
		{
			cout << setw(8) << EMPLOYEE[i].getName();
			cout << setw(8) << EMPLOYEE[i].getIdnumber();
			cout << setw(8) << EMPLOYEE[i].getDepartment();
			cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
		}
		return 0;
}

The ERRORS:
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. : see declaration of 'strcpy'
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.see declaration of 'strcpy'
error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [13], int, const char [11], const char [15])'
'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types
: could be 'EmployeeData::EmployeeData(char *,int,char,char) while trying to match the argument list '(const char [11], int, const char [3], const char [11])'
: error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument typesclass.cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [11], int, const char [14], const char [9])'

Sorry about that i posted it in the wrong place, sorry once again.
I have been working on this all night, i'm like right there in getting it to work, could you guys tell me what these errors mean, and how to fix them so that i can get this program working. Thanks you.

This is what i have for the file employee class:

#ifndef EmployeeClass
#define EmployeeClass
#include < cstring>

const int DEFAULT_SIZE = 51;

class EmployeeData
{
private:
	char *name;
	int idnumber;
	char department;
	char position;

	//private member function
	void createName(int size, char *value)
	{//Allocate the default amount of memory for name
		name = new char [size];

		//store a value in the memory
		strcpy(name, value);}

public:
	//Constructor #1
	EmployeeData()
	{//Store an empty string in the name attribute.
		createName(DEFAULT_SIZE, "");

		//initialize idnumber department position
		idnumber = 0;
		department = 0;
		position = 0;}

	//Constructor #2
	EmployeeData(char *nam)
	{//allocate memory and store the description.
		createName(strlen(nam), nam);

		//initialize idnumber department position
		idnumber = 0;
		department = 0;
		position = 0;}
	
	//Constructor #3
	EmployeeData(char *nam, int id, char d, char p)
	{//Allocate memory and store the description.
		createName(strlen(nam), nam);

		//Assign values to id deparment position
		idnumber = id;
		department = d;
		position = p;}

	//Destructor
	~EmployeeData()
	{ delete[] name;}

	//mutator functions
	void setName(char *n)
	{strcpy(name, n);}

	void setIdnumber(int id)
	{idnumber = id;}

	void setDepartment(char d)
	{department = d;}

	void setPosition(char p)
	{position =p;}

	//Accessor functions
	const char *getName() const
	{return name;}

	int getIdnumber() const
	{return idnumber;}

	char getDepartment() const
	{return department;}

	char getPosition() const
	{return position;}
};
#endif

This is what i have for the file Data Two:

#include <iostream>
#include <iomanip>
#include "Employee Class.cpp"
using namespace std;

int main()
{
	const int NUM_WORKERS = 3;
	EmployeeData EMPLOYEE[NUM_WORKERS] ={
		EmployeeData("Susan Meyers", 47899, "Accounting", "Vice President"),
		EmployeeData("Mark Jones", 39119, "IT", "Programmer"),
		EmployeeData("Joy Rogers", 81774, "Manufacturing", "Engineer")};

		cout << " NAME	ID NUMBER	DEPARTMENT	POSITION\n";
		cout << "-------------------------------------------\n";

		for (int i = 0; i < NUM_WORKERS; i++)
		{
			cout << setw(8) << EMPLOYEE[i].getName();
			cout << setw(8) << EMPLOYEE[i].getIdnumber();
			cout << setw(8) << EMPLOYEE[i].getDepartment();
			cout << setw(8) << EMPLOYEE[i].getPosition() << endl;
		}
		return 0;
}

The ERRORS:
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. : see declaration of 'strcpy'
warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead.see declaration of 'strcpy'
error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [13], int, const char [11], const char [15])'
'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument types
: could be 'EmployeeData::EmployeeData(char *,int,char,char) while trying to match the argument list '(const char [11], int, const char [3], const char [11])'
: error C2665: 'EmployeeData::EmployeeData' : none of the 4 overloads could convert all the argument typesclass.cpp(50): could be 'EmployeeData::EmployeeData(char *,int,char,char)'
while trying to match the argument list '(const char [11], int, const char [14], const char [9])'

//THIS IS THE DATA TWO FILE/PROGRAM
#include <iostream>
#include <iomanip>
#include "Employee Class.cpp"
using namespace std;

int main()
{
	const int NUM_WORKERS = 3;
	EmployeeData EMPLOYEE[NUM_WORKERS] ={
		EmployeeData("Susan Meyers", 47899, "Accounting", "Vice President"),
		EmployeeData("Mark Jones", 39119, "IT", "Programmer"),
		EmployeeData("Joy Rogers", 81774, "Manufacturing", "Engineer")};

		cout << "     NAME    ID NUMBER	    DEPARTMENT   	   POSITION\n";
		cout << "-----------------------------------------------------------------\n";

		for (int i = 0; i < NUM_WORKERS; i++)
		{
			cout << setw(12) << EMPLOYEE[i].getName();
			cout << setw(8) << EMPLOYEE[i].getIdnumber();
			cout << setw(20) << EMPLOYEE[i].getDepartment();
			cout << setw(20) << EMPLOYEE[i].getPosition() << endl;
		}
		return 0;
}

//THIS IS THE OTHER FILE/PROGRAM EMPLOYEE CLASS.CPP

#ifndef EmployeeClass
#define EmployeeClass
#include <cstring>

const int DEFAULT_SIZE = 51;

class EmployeeData
{
private:
	char *name;
	int idnumber;
	char *department;
	char *position;

	//private member function
	void createName(int size, char *value)
	{//Allocate the default amount of memory for name
		name = new char [size];

		//store a value in the memory
		strcpy(name, value);}

	void createDepartment(int size, char *value2)
	{
		department = new char [size];
		strcpy(department, value2);}

	void createPosition(int size, char *value3)
	{
		position = new char [size];
		strcpy(position, value3);}

public:
	//Constructor #1
	EmployeeData()
	{//Store an empty string in the name attribute.
		createName(DEFAULT_SIZE, "");
		createDepartment(DEFAULT_SIZE, "");
		createPosition(DEFAULT_SIZE, "");

		//initialize idnumber department position
		idnumber = 0;}

	//Constructor #2
	EmployeeData(char *nam, char *dep, char *pos)
	{//allocate memory and store the description.
		createName(strlen(nam), nam);
		createDepartment(strlen(dep), dep);
		createPosition(strlen(pos), pos);

		//initialize idnumber department position
		idnumber = 0;}
	
	//Constructor #3
	EmployeeData(char *nam, int id, char *dep, char *pos)
	{//Allocate memory and store the description.
		createName(strlen(nam), nam);
		createDepartment(strlen(dep), dep);
		createPosition(strlen(pos), pos);

		//Assign values to id deparment position
		idnumber = id;}

	//Destructor
	~EmployeeData()
	{ delete[] name;}

	//mutator functions
	void setName(char *n)
	{strcpy(name, n);}

	void setIdnumber(int id)
	{idnumber = id;}

	void setDepartment(char *d)
	{strcpy (department,d);}

	void setPosition(char *p)
	{strcpy (position,p);}

	//Accessor functions
	const char *getName() const
	{return name;}

	int getIdnumber() const
	{return idnumber;}

	char *getDepartment() const
	{return department;}

	char *getPosition() const
	{return position;}
};
#endif

I HAVE MANAGED TO GET IT TO INITIALIZE AND GIVE ME THE SCREEN, BUT IN THE PROCESS OF TRYING TO START, I GET A DEBUG ERROR POP UP SCREEN TELLING ME THAT: HEAP CORRUPTION DETECTED AFTER NORMAL BLOCK (#126) AT 0X0073FE70 CRT DETECTED THAT THE APPLICATION WROTE TO MEMORY AFTER END OF HEAP BUFFER.
After i press the button abort it will leave the display screen, otherwaise it will close the screen by it self. How do i get rid of this pop up error.

Thank you guys who are helping me, i just need a little extra push to get this working.

Line 3 - Someone mentioned this earlier. Get rid of the space.

Should be:

#include "EmployeeClass.cpp"

not

#include "Employee Class.cpp"

Don't mistake a warning for an error:

warning C4996: 'strcpy': This function or variable may be unsafe. Consider using strcpy_s instead. : see declaration of 'strcpy'

Ignore it for now.

As for the errors, here's the closest thing you have in your constructors:

EmployeeData(char *nam, int id, char d, char p)

Here's your call:

EmployeeData("Susan Meyers", 47899, "Accounting", "Vice President")

It doesn't match. Try writing a constructor like this:

EmployeeData(char* nam, int id, char* d, char* p)

Thanks, vernon. In the new posted, that i had done shortly after that one, i fixed that part, i realized that i was missing the pointers and have already repaired that section. I also reposted the new code to reflect all the changes, if you are still there and read this hopefully you can help me. After i run the program it will load the display screen but it gives me a pop up error and it will only leave the screen for display after i select aboart for the error. Do you see anything else with the new code that might cause that pop up error. Thanks.

As to your updated code, at the very least, you aren't providing enough space. You provide room for all the characters, but not the null terminator.

void createName(int size, char *value)
	{//Allocate the default amount of memory for name
		name = new char [size];

		//store a value in the memory
		strcpy(name, value);}

Function call in red:

EmployeeData(char *nam, char *dep, char *pos)
	{//allocate memory and store the description.
		createName(strlen(nam), nam);
		createDepartment(strlen(dep), dep);
		createPosition(strlen(pos), pos);

		//initialize idnumber department position
		idnumber = 0;}

strlen returns the number of character NOT counting the null terminator.

http://www.cplusplus.com/reference/clibrary/cstring/strlen/

So at the very minimum, you have a problem there. Not enough storage. I'm not saying that fixing that will solve everything or that it is the ONLY problem, but fix it and see if that solves everything.

commented: Really, good teacher, and also gives examples, and links to better understand what you are doing wrong. +1

Hum, what do you know. it worked, you are really something, vernon. it fixed the problem. thanks. and thanks for the link, it helped.

For some reason, it loaded after i made those changes, but when i saved the file and closed it. When i whent to open it up again, it doens't show any errors or nothing, it just appears and as quick as it appears it closes itself, only when i change the size again where vernon showed me to make sure i have enough space. it will work but once i close it and open the file again it comes on and off really fast with no errors.

if I understand correctly, try adding this just before your return statement in "int main()"

cin.get();

that will wait for you to review the data and press enter before exiting the program. As it is now as soon as it's done with all of it's tasks, it closes instantly.

~J

Thanks I used the pause statement, i remembered about it when watching a movie. funny how things come to ones mind when it takes a break. But thank you .

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.