Employee Class #2 pg802 SOWC++ 8th edit.

So here's the thing...I'm still learning to deal with classes, I was told to do this program for class and so far I think I'm doing well, but i got completely block when it told me to write a code that creates three objects. I'm not exactly sure how to do that. Can someone help me out?

The instructions and the code are below so you guys can see what I've done so far. I'd like to learn as much as possible since "Programing 2" will be completely of "Object Oriented programming", I've been told.



2. Employee Class
Write a class named Employee that has the following member variables:
• name. A string that holds the employee’s name.
• idNumber. An int variable that holds the employee’s ID number.
• department. A string that holds the name of the department where the employee
works.
• position. A string that holds the employee’s job title.
The class should have the following constructors:
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name, employee’s ID number, department,and position.
• A constructor that accepts the following values as arguments and assigns them to the appropriate member variables: employee’s name and ID number. The department and position fields should be assigned an 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 and accessor functions that return the values in these member variables. Once you have written the class, write a separate program that creates three Employee objects to hold the following data.

Name ID Number Department Position
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 each employee on the screen.

Program:

employee.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
#pragma once
#include<iostream>
using namespace::std;

class Employee {
private:
	string name,
		   department,
		   position;

	int idNumber;

public:

	Employee();
	Employee(double, string, string, string);
	~Employee();

	void setname(string);
	void setidNumber(double);
	void setdepartment(string);
	void setposition(string);
	string getname() const;
	double getidNumber() const;
	string setdepartment() const;
	string setposition() const;
};


Employee.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
#include"Employee.h"


Employee:: Employee() {
	
	idNumber =  0.0;
	name = "";
	department = "";
	position = "";
}

Employee::Employee(double numero, string nombre, string departamento, string posicion) {
	
	name = nombre;
	idNumber = numero;
	department = departamento;
	position = posicion;
	
}
Employee::Employee(double numero, string nombre, string departamento, string posicion) {

	name = nombre;
	idNumber = numero;
	department = "";
	position = "";

}

Employee::~Employee() {

}

void Employee::setname(string nombre) {
	name = nombre;
}
void Employee::setidNumber(double numero) {
	idNumber = numero;
}
void Employee::setdepartment(string departamento) {
	department = departamento;
}
void Employee::setposition(string posicion) {
	position = posicion;
}
string Employee::getname() const {
	return name;
}
double Employee::getidNumber() const {
	return idNumber;
}
string Employee::setdepartment() const {
	return department;
}
string Employee::setposition() const {
	return position;
}


Employee_main.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

#include<iostream>
#include<string>
#include"Employee.h"
using namespace::std;


int main() {
	string nombre, departamento, posicion;
	double numero;

	cout << "Enter your name: ";
	cin >> nombre;
	cout << "Enter your Id number: ";
	cin >> numero;
	cout << "Enter your department: ";
	cin >> departamento;
	cout << "Enter your posicion: ";
	cin >> posicion;

	



	system("pause");
	return 0;
}



Enter your name: angel
Enter your Id number: 1
Enter your department: Enter your position:


I'm almost done, but this is the output I'm getting and I don't see any problem with the code, please help! =(


here's the code so far...

employee.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
#pragma once
#include<iostream>
using namespace::std;

class Employee {
private:
	string name,
		   department,
		   position;

	int idNumber;

public:

	Employee();
	Employee(int, string, string, string);
	Employee(int, string);
	~Employee();

	void setname(string);
	void setidNumber(int);
	void setdepartment(string);
	void setposition(string);
	string getname() const;
	int getidNumber() const;
	string getdepartment() const;
	string getposition() const;
};


employee.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
#include"Employee.h"


Employee:: Employee() {
	
	idNumber =  0;
	name = "";
	department = "";
	position = "";
}

Employee::Employee(int numero, string nombre, string departamento, string posicion) {
	
	name = nombre;
	idNumber = numero;
	department = departamento;
	position = posicion;
	
}
Employee::Employee(int numero, string nombre) {

	name = nombre;
	idNumber = numero;
	department = "";
	position = "";

}

Employee::~Employee() {

}

void Employee::setname(string nombre) {
	name = nombre;
}
void Employee::setidNumber(int numero) {
	idNumber = numero;
}
void Employee::setdepartment(string departamento) {
	department = departamento;
}
void Employee::setposition(string posicion) {
	position = posicion;
}
string Employee::getname() const {
	return name;
}
int Employee::getidNumber() const {
	return idNumber;
}
string Employee::getdepartment() const {
	return department;
}
string Employee::getposition() const {
	return position;
}


employee_main.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
#include<iostream>
#include<string>
#include"Employee.h"
using namespace::std;


int main() {
	string nombre, departamento, posicion;
	int numero;
	
	Employee persona[3];
	for (int i = 0; i < 3; i++) {
		cout << "Enter your name: ";
		getline(cin,nombre);
		persona[i].setname(nombre);
		cout << "Enter your Id number: ";
		cin >> numero;
		persona[i].setidNumber(numero);
		cout << "Enter your department: ";
		getline(cin, departamento);
		persona[i].setdepartment(departamento);
		cout << "Enter your position: ";
		getline(cin, posicion);
		persona[i].setposition(posicion);
	}
	
	cout << "Name" << "\t" << "Id Number" << "\t" << "Department" << "\t" << "Position" << endl;

	for (int i = 0; i < 3; i++) {
		cout << persona[i].getname() << "\t" << persona[i].getidNumber() << "\t" << persona[i].getdepartment() << "\t" << persona[i].getposition() << endl;
	}
	cout << endl;


	system("pause");
	return 0;
}

Last edited on
This problem occurs because you're switching between the extraction operator>> and getline(). The extraction operator>> leaves the new line character in the stream which getline() will retrieve and then consider the operation complete. So what you need to do is to consume that new line character before you try to retrieve the string. One way is to use the istream.ignore() function to empty the input buffer. See http://www.cplusplus.com/reference/istream/istream/ignore/ for more information.
Topic archived. No new replies allowed.