Array of objects - CODe Mode

Pages: 12
Hello all,

I am trying to make an employee program that takes in up to 100 max employees information then displays it back out nicely formatted, which I am not getting right and have been googling wtf I am doing wrong. Also I was wondering after inputting all the employee records how can I take and get an average of the companies age and income...if someone can please guide me in the right direction as I have been at this for 2 days really trying to learn and not sure what I am doing wrong. Thank you.

This is what i have so far:

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

class Employee {
	string employeeName;
	string employeeTitle;
	int employeeAge;
	long employeeSalary;
public:
	void get_data(string, string, int, long);
	void data_analysis();
	void display_employee_list();
	void display_stats();

}; 

void Employee::get_data(string ename, string etitle, int eage, long esalary) 
{
	employeeName = ename;
	employeeTitle = etitle;
	employeeAge = eage; 
	employeeSalary = esalary; 
}
void Employee::display_employee_list()
{
	
	cout<<employeeName << setw(20) << employeeTitle <<setw(19)<<employeeAge <<setw(21)<<employeeSalary<<endl;
	
}

int main ()
{
	
	Employee employees[100];
	int n = 0;
	string ename, etitle;
	int eage;
	long esalary;
	char again;
	do {
	cout <<setprecision(2)
		 <<setiosflags(ios::fixed)
		 <<setiosflags(ios::showpoint);
	cout <<"Please enter employee name: ";
	getline(cin, ename);
	cout << endl;
	cout <<"Please enter employee title: ";
	getline(cin, etitle);
	cout << endl;
	cout <<"Please enter employee age: ";
	cin >> eage;
	cout << endl;
	cout <<"Please enter employee salary: ";
	cin >> esalary;
	cout << endl; 
	employees[n++].get_data(ename, etitle,eage,esalary);
	cout <<"Next Employee? Type n or N to stop the data entry.  "; 
	cin >> again;
	cin.ignore();
	cout <<endl;
	} while(again =='y'); 
	cout << "\t\tEmployee Statistical Report:" << endl;
	cout <<endl;
	cout <<"Employee Name" <<setw(20)<<"Employee Title" <<setw(18)<< "Age" <<setw(20) <<"Salary" <<endl;
	for (int i=0; i<n; i++)
	{
		employees[i].display_employee_list();
		
	}
	cout <<endl;
	system("PAUSE");

return 0; 
}
Last edited on
Lol! Nice output! haha..
Anyways..

*Don't use system("PAUSE"); unless you are making the program for windows only.

So. What's going wrong? Well.. I don't like how you display the title too much, but it is fine. So what's causing the awkward tabs for the other outputs? Well. You don't have a setw() for "Employee Name" and the corresponding employeeName. Make it something sensible like 20 or something, then try your program again. Don't like everything being justified to the right? Then use the function 'left' when outputting.

 
cout << left << setw(20) << employeeName << setw(20) . . . ;


and

 
cout << left << setw(20) << "Employee Name" <<setw(20) . . . ;
Last edited on
Thank you for your prompt response back, I will be trying this momentarily.

What really is causing me to lose sleep though is after collecting all the data from various objects how can I calculate an average of age and salary? I am getting lost on how to step through the array of objects stored to determine such output.

Well, that depends on how you want to do it.

Here is an example:
1
2
3
4
5
6
7
8
9
int totalAge = 0;
int averageAge = 0;

for (int index = 0; index < NumberOfEmployees; index++)
{
	totalAge += employees[index].employeeAge;
}

averageAge = totalAge/NumberOfEmployees;


You will have to keep track of how many employees are actually input.
Thank you for the guide in the right direction as your formatting worked great, but I am stuck again I have gone through my book and research for options, I understand that the object declared knows how many records are stored. I am not understanding how to define employees and NumberOfemployees. Pulling my hair out here really trying to learn, hopefully I can do this. :>

Christine

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

class Employee {
	string employeeName;
	string employeeTitle;
	int employeeAge;
	long employeeSalary;
public:
	void get_data(string, string, int, long);
	void data_analysis();
	void display_employee_list();
	void display_stats();

}; 

void Employee::get_data(string ename, string etitle, int eage, long esalary) 
{
	employeeName = ename;
	employeeTitle = etitle;
	employeeAge = eage; 
	employeeSalary = esalary; 
}
void data_analysis()
{
	int totalAge = 0;
    int averageAge = 0;

for (int index = 0; index < NumberOfEmployees; index++)
{
	totalAge += employees[index].employeeAge;
}

averageAge = totalAge/NumberOfEmployees;

}
void Employee::display_employee_list()
{
	
	cout<< left << setw(20) << employeeName << setw(20) << employeeTitle <<setw(20)<<employeeAge <<setw(21)<<employeeSalary<<endl;
	
}
void display_stats()
{
	cout << "The average age is: " <<averageAge; //cannot pass variable from member function to another member function? 

}

int main ()
{
	
	Employee employees[100];
	int n = 0;
	string ename, etitle;
	int eage;
	long esalary;
	char again;
	do {
	cout <<setprecision(2)
		 <<setiosflags(ios::fixed)
		 <<setiosflags(ios::showpoint);
	cout <<"Please enter employee name: ";
	getline(cin, ename);
	cout << endl;
	cout <<"Please enter employee title: ";
	getline(cin, etitle);
	cout << endl;
	cout <<"Please enter employee age: ";
	cin >> eage;
	cout << endl;
	cout <<"Please enter employee salary: ";
	cin >> esalary;
	cout << endl; 
	employees[n++].get_data(ename, etitle,eage,esalary);
	cout <<"Next Employee? Type n or N to stop the data entry.  "; 
	cin >> again;
	cin.ignore();
	cout <<endl;
	} while(again =='y'); 
	cout << "\t\tEmployee Statistical Report:" << endl;
	cout <<endl;
	cout << left << setw(20) << "Employee Name" << left <<setw(20)<<"Employee Title" << left <<setw(15)<< "Age" <<setw(20) <<"Salary" <<endl;
	for (int i=0; i<n; i++)
	{
		employees[i].display_employee_list();
		employees[i].data_analysis(); // do i need to passing something here to get employees index and number of employees?
		
		
	}
	cout <<endl;
	system("PAUSE");

return 0; 
}

You did employees correctly if you want to limit the number of employees to 100.
This is fine. You would have to use dynamic memory if you want the user to decide.

But whichever way you go, keeping track of NumberOfEmployees is not very hard:
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
Employee employees[100]; //prepares 100 memory locations for 100 Employee objects
int numberOfEmployees = 0; //start with 0 Employee objects
. . . . .
do
{
    numberOfEmployees ++; //add 1 to the number each time

    cout << setprecision ( 2 ) << setiosflags ( ios::fixed ) << setiosflags ( ios::showpoint );
    cout << "Please enter employee name: ";
    getline ( cin, ename );
    cout << endl;

    cout << "Please enter employee title: ";
    getline ( cin, etitle );
    cout << endl;

    cout << "Please enter employee age: ";
    cin >> eage;
    cout << endl;

    cout << "Please enter employee salary: ";
    cin >> esalary;
    cout << endl;

    employees[n++].get_data ( ename, etitle, eage, esalary );
    cout << "Next Employee? Type n or N to stop the data entry.  ";
    cin >> again;
    cin.ignore ( );
    cout << endl;
}
while ( again == 'y' );
. . . . .
I did want to limit the number of employees to 100.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

class Employee {
	string employeeName;
	string employeeTitle;
	int employeeAge;
	long employeeSalary;
public:
	void get_data(string, string, int, long);
	void data_analysis();
	void display_employee_list();
	void display_stats(int, Employee);

}; 

void Employee::get_data(string ename, string etitle, int eage, long esalary) 
{
	employeeName = ename;
	employeeTitle = etitle;
	employeeAge = eage; 
	employeeSalary = esalary; 
}
void data_analysis(int NumberOfEmployees, Employee employees) // is this how to bring in data for loop?
{
	int totalAge = 0;
    int averageAge = 0;

for (int index = 0; index < NumberOfEmployees; index++)
{
	totalAge += employees[index].employeeAge;
}

averageAge = totalAge/NumberOfEmployees;

}
void Employee::display_employee_list()
{
	
	cout<< left << setw(20) << employeeName << setw(20) << employeeTitle <<setw(20)<<employeeAge <<setw(21)<<employeeSalary<<endl;
	
}
void display_stats()
{
	cout << "The average age is: " <<averageAge; //cannot pass variable from member function to another member function? 

}

int main ()
{
	
	Employee employees[100];
	int numberOfEmployees = 0;
	int n = 0;
	string ename, etitle;
	int eage;
	long esalary;
	char again;
	do {
	numberOfEmployees ++;
	cout <<setprecision(2)
		 <<setiosflags(ios::fixed)
		 <<setiosflags(ios::showpoint);
	cout <<"Please enter employee name: ";
	getline(cin, ename);
	cout << endl;
	cout <<"Please enter employee title: ";
	getline(cin, etitle);
	cout << endl;
	cout <<"Please enter employee age: ";
	cin >> eage;
	cout << endl;
	cout <<"Please enter employee salary: ";
	cin >> esalary;
	cout << endl; 
	employees[n++].get_data(ename, etitle,eage,esalary);
	cout <<"Next Employee? Type n or N to stop the data entry.  "; 
	cin >> again;
	cin.ignore();
	cout <<endl;
	} while(again =='y'); 
	cout << "\t\tEmployee Statistical Report:" << endl;
	cout <<endl;
	cout << left << setw(20) << "Employee Name" << left <<setw(20)<<"Employee Title" << left <<setw(15)<< "Age" <<setw(20) <<"Salary" <<endl;
	for (int i=0; i<n; i++)
	{
		employees[i].display_employee_list();
		employees[i].data_analysis(numberOfEmployees, employees); //eror too many arguments? 
		
		
	}
	cout <<endl;
	system("PAUSE");

return 0; 
}


I probably am doing something wrong and do not see the structure I am failing to take.
Yeah. You are still testing i against n. What is n? Well I'll tell you what it isn't. It isn't the number of employees.

This is why we use words that help us describe what a variable is.
If someone looks at your code and sees n, they will ask what the heck n is.
If someone sees numberOfEmployees, then they know what it is.

If you want to change the name of a variable, do a simple search for numberOfEmployees, you find all the variables you need to change. Do a search for 'n' guess what, you find a bajillion ns in your code. Same goes for 'i' which is why I use index.
Last edited on
I have no idea what they are. Wow I am just terrible at this.
Haha. Are you using this code from somewhere else? Or did you make it yourself?

You can't say you're terrible. You just haven't gotten into the habit of checking for this stuff.
But you will one day.
I know you are right. I want to be able to understand this so bad and day in and day out goes by of me just reading and reading and same things over and over and I don't know what to say.

I wrote this code completely from scratch that is suppose to meet criteria of an assignment.
Then read a book. I read two C++ books in one day (and learned a lot). They aren't hard to read, and 500 pages isn't too long if you remember what you read and apply it to what you're reading.

I don't have any digital C++ books, but there are plenty of great tutorials online. Read them. Read forums where people argue about C++ stuff. The easiest way to get many different ways to do something is posting on a good C++ forum that such and such way is the best.

Read read read. Don't try until you read. There is no try. You must know before you do. Otherwise you are wasting precious time. Don't waste time reinventing the wheel. Waste time adding to the work of others to make a better wheel. But you need to know what they did first in order to improve it.
Last edited on
Are they properly labeled now? If so still not realizing how to get everything to data_analysis() then display_stats().

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

class Employee {
	string employeeName;
	string employeeTitle;
	int employeeAge;
	long employeeSalary;
public:
	void get_data(string, string, int, long);
	void data_analysis();
	void display_employee_list();
	void display_stats(int, Employee);

}; 

void Employee::get_data(string ename, string etitle, int eage, long esalary) 
{
	employeeName = ename;
	employeeTitle = etitle;
	employeeAge = eage; 
	employeeSalary = esalary; 
}
void data_analysis(int NumberOfEmployees, Employee employees) // is this how to bring in data for loop?
{
	int totalAge = 0;
    int averageAge = 0;

for (int index = 0; index < NumberOfEmployees; index++)
{
	totalAge += employees[index].employeeAge;
}

averageAge = totalAge/NumberOfEmployees;

}
void Employee::display_employee_list()
{
	
	cout<< left << setw(20) << employeeName << setw(20) << employeeTitle <<setw(20)<<employeeAge <<setw(21)<<employeeSalary<<endl;
	
}
void display_stats()
{
	cout << "The average age is: " <<averageAge; //cannot pass variable from member function to another member function? 

}

int main ()
{
	
	Employee employees[100];
	int numberOfEmployees = 0;
	int employeesSubscript = 0;
	string ename, etitle;
	int eage;
	long esalary;
	char again;
	do {
	numberOfEmployees ++;
	cout <<setprecision(2)
		 <<setiosflags(ios::fixed)
		 <<setiosflags(ios::showpoint);
	cout <<"Please enter employee name: ";
	getline(cin, ename);
	cout << endl;
	cout <<"Please enter employee title: ";
	getline(cin, etitle);
	cout << endl;
	cout <<"Please enter employee age: ";
	cin >> eage;
	cout << endl;
	cout <<"Please enter employee salary: ";
	cin >> esalary;
	cout << endl; 
	employees[employeesSubscript++].get_data(ename, etitle,eage,esalary);
	cout <<"Next Employee? Type n or N to stop the data entry.  "; 
	cin >> again;
	cin.ignore();
	cout <<endl;
	} while(again =='y'); 
	cout << "\t\tEmployee Statistical Report:" << endl;
	cout <<endl;
	cout << left << setw(20) << "Employee Name" << left <<setw(20)<<"Employee Title" << left <<setw(15)<< "Age" <<setw(20) <<"Salary" <<endl;
	for (int counter=0; counter<employeesSubscript; counter++)
	{
		employees[counter].display_employee_list();
		employees[counter].data_analysis(numberOfEmployees, employees[i]); //eror too many arguments? 
		
		
	}
	cout <<endl;
	system("PAUSE");

return 0; 
}
Oh I wasn't looking at data_analysis function.

The problem is simple. data_analysis function is not part of the Employee class, correct? Therefore, it cannot access private members of Employee objects. employeeAge is a private member.

You never call display_stats function. Also. averageAge in that function does NOT exist. You declare averageAge in the data_analysis function. Which is fine. But the display_stats function does not have an averageAge. Read of 'scope' on google or something.

employees[i] you forgot to change this to employees[counter].

system("PAUSE"); it is bothering me. Get rid of it. Instead, read this thread:
http://www.cplusplus.com/forum/beginner/1988/

Yeah, I know its long. But you don't have to read all of it. You should get your answer in the first few pages. Maybe the first page.
I made all the functions, member functions. Also made some updates, still not getting to being able to calculate, once I can figure this out, I am to calculate average income, but should be able to, once I get a solution for the age.

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
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iostream>
#include <string>
#include <iomanip>

using namespace std; 

class Employee {
	string employeeName;
	string employeeTitle;
	int employeeAge;
	long employeeSalary;
public:
	void get_data(string, string, int, long);
	void data_analysis();
	void display_employee_list();
	void display_stats();

}; 

void Employee::get_data(string ename, string etitle, int eage, long esalary) 
{
	employeeName = ename;
	employeeTitle = etitle;
	employeeAge = eage; 
	employeeSalary = esalary; 
}
void Employee::data_analysis(int NumberOfEmployees, Employee employees[counter]) // is this how to bring in data for loop?
{
	int totalAge = 0;
    int averageAge = 0;

for (int index = 0; index < NumberOfEmployees; index++)
{
	totalAge += employees[counter].employeeAge;
}

averageAge = totalAge/NumberOfEmployees;

}
void Employee::display_employee_list()
{
	
	cout<< left << setw(20) << employeeName << setw(20) << employeeTitle <<setw(20)<<employeeAge <<setw(21)<<employeeSalary<<endl;
	
}
void Employee::display_stats()
{
	cout << "The average age is: " <<averageAge; //Still cannot pass variable from member function to another member function? 

}

int main ()
{
	
	Employee employees[100];
	int numberOfEmployees = 0;
	int employeesSubscript = 0;
	string ename, etitle;
	int eage;
	long esalary;
	char again;
	do {
	numberOfEmployees ++;
	cout <<setprecision(2)
		 <<setiosflags(ios::fixed)
		 <<setiosflags(ios::showpoint);
	cout <<"Please enter employee name: ";
	getline(cin, ename);
	cout << endl;
	cout <<"Please enter employee title: ";
	getline(cin, etitle);
	cout << endl;
	cout <<"Please enter employee age: ";
	cin >> eage;
	cout << endl;
	cout <<"Please enter employee salary: ";
	cin >> esalary;
	cout << endl; 
	employees[employeesSubscript++].get_data(ename, etitle,eage,esalary);
	cout <<"Next Employee? Type n or N to stop the data entry.  "; 
	cin >> again;
	cin.ignore();
	cout <<endl;
	} while(again =='y'); 
	cout << "\t\tEmployee Statistical Report:" << endl;
	cout <<endl;
	cout << left << setw(20) << "Employee Name" << left <<setw(20)<<"Employee Title" << left <<setw(15)<< "Age" <<setw(20) <<"Salary" <<endl;
	for (int counter=0; counter<employeesSubscript; counter++)
	{
		employees[counter].display_employee_list();
		employees[counter].data_analysis(numberOfEmployees, employees[counter]); //cannot seem to pass in numberOfemployees or employees 
		
		
	}
	cout <<endl;
	cin.get();

return 0; 
}
Ok. First off. You need to get something out of your head. Variables that you create in function1 CANNOT be used in function2. You need to pass the variables around some way. You can't just access them from one function to the next.
I understand that is due to the scope of things, but this goes even so with member functions?
It goes with everything my friend. Functions don't change the way they work just cus they are inside a class. They all work the same. The ONLY difference is that member functions in a class, can access the member variables of their CLASS. That is it.

*But this is due to scope of course. Not something special that functions get.
Last edited on
Okay that makes sense. But how in the world can I process this data..
Very carefully. It will probably take some major design changes to your code.
Pages: 12