employee database using vectors problem

Pages: 12
i am supposed to write a program for employee database with following details:
1. All employee have first name,last name and salary.
2.Engineers in addition to employees have details of their trade, years of experience and whether they know c++ or not.
3.Managers in addition to employees have details of number of meetings and number of vacations
4.Researchers have details their school of Phd. and its topic.
Here is what i have done 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
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//employee.h
#ifndef EMPLYOEE_H
#define EMPLOYEE_H
#include<iostream>
#include<fstream>
#include<string>
using namespace std;
class Employee
{
public:
	Employee(string firstName,string lastName,int salary)
	{mFirstName=firstName;mLastName=lastName;mSalary=salary;}
	virtual void printStats()
	{
		cout<<"First name:"<<mFirstName<<endl;
		cout<<"Last name:"<<mLastName<<endl;
		cout<<"Salary:"<<mSalary<<endl;
	} 
	virtual void save(ofstream& outFile)
	{
		outFile<<"First name:"<<mFirstName<<endl;
		outFile<<"Last name:"<<mLastName<<endl;
		outFile<<"Salary:"<<mSalary<<endl;
	}
	string getLastName()
	{ return mLastName;}
protected:
	string mFirstName;
	string mLastName;
	int mSalary;
};
class Engineer: public Employee
{
public:
	Engineer(string firstName,string lastName,int salary,string knowCPP,int yearsExp,string trade):Employee(firstName,lastName,salary)
	{
		mKnowCPP=knowCPP;mYearsExp=yearsExp;mtrade=trade;}
	void Save(ofstream& outFile)
	{
		Employee::save(outFile);
		outFile<<"know c++:"<<mKnowCPP<<endl;
		outFile<<"Years of exp:"<<mYearsExp<<endl;
		outFile<<"trade:"<<mtrade<<endl;
	}
	void PrintStats()
	{
		Employee::printStats();
		cout<<"know c++:"<<mKnowCPP<<endl;
		cout<<"Years of exp:"<<mYearsExp<<endl;
		cout<<"trade:"<<mtrade<<endl;
	}
protected:
	string mFirstName;
	string mLastName;
	int mSalary;
	string mKnowCPP;
	int mYearsExp;
	string mtrade;
};
class Manager: public Employee
{
public:
	Manager(string firstName,string lastName,int salary,int meet,int vac):Employee(firstName,lastName,salary)
	{mMeet=meet;mVac=vac;}
	void Save(ofstream& outFile)
	{
		Employee::save(outFile);
		outFile<<"Number of meetings:"<<mMeet<<endl;
		outFile<<"Number of holidays:"<<mVac<<endl;
	}
	void printStats()
	{
		Employee::printStats();
		cout<<"Number of meetings:"<<mMeet<<endl;
		cout<<"Number of holidays:"<<mVac<<endl;
	}
protected:
	int mMeet;
	int mVac;
};
class Researcher:public Employee
{
public:
	Researcher(string firstName,string lastName,int salary,string schoolPhd,string PhdTopic):Employee(firstName,lastName,salary)
	{mSchoolPhd=schoolPhd;mPhdTopic=PhdTopic;}
	void save(ofstream& outFile)
	{
		Employee::save(outFile);
		outFile<<"School of Phd.:"<<mSchoolPhd<<endl;
		outFile<<"Phd. topic:"<<mPhdTopic<<endl;
	}
protected:
	string mSchoolPhd;
	string mPhdTopic;
};

#endif 

here is my main file:
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
100
101
102
103
104
105
//main().cpp
#include<iostream>
#include<fstream>
#include<vector>
#include<string>
#include"employee.h"
using namespace std;
int main()
{
	vector<Employee*> database;
	bool quit=false;
	ofstream outFile("out.txt");
	Employee* emp[50];
	int input,select1,j,sal,vac,meet,exp;
	string firstName;string lastName;string school;string topic;string knowCPP;string trade;string search;
	while(!quit)
	{
		cout<<"Database size:"<<database.size()<<endl;
		cout<<"Database contains:";
		for(int k=0;k<database.size();k++)
		{
			emp[k]->printStats();
			cout<<endl<<endl;
		}
		cout<<"\n1.Add an employee 2.Delete an employee 3. Exit ";
		cin>>select1;
		cout<<endl;
		switch(select1)
		{
		case 1:
			cout<<"\n1.Add a manager 2. Add an engineer 3.Add a researcher ";
			cin>>input;
			switch(input)
			{
			case 1:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cout<<"\nEnter number of meetings in an year:";
				cin>>meet;
				cout<<"\nEnter number of vacations in year:";
				cin>>vac;
				emp[j]=new Manager(firstName,lastName,sal,meet,vac);
				database.push_back(emp[j]);
				emp[j]->save(outFile);
				j++;
				break;
			case 2:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cout<<"\nEnter whether know c++ y/n";
				cin>>knowCPP;
				cout<<"\nEnter trade:";
				cin>>trade;
				cout<<"\nEnter years of experience:";
				cin>>exp;
				emp[j]= new Engineer(firstName,lastName,sal,knowCPP,exp,trade);
				database.push_back(emp[j]);
				emp[j]->save(outFile);
				j++;
				break;
			case 3:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cout<<"\nEnter the name of school of Phd.:";
				getline(cin,school);
				cout<<"\nEnter the topic pf Phd.:";
				getline(cin,topic);
				emp[j]=new Researcher(firstName,lastName,sal,school,topic);
				database.push_back(emp[j]);
				emp[j]->save(outFile);
				j++;
				break;
			}
			break;
		case 2:
			cout<<"enter last name of employee to be deleted:";
			getline(cin,search);
			for(int l=0;l<database.size();l++)
			{
				if(database[l]->getLastName()==search)
				{
					database.erase(database.begin()+l);
					break;
				}
			}
		case 3:
			cout<<"/nExiting............";
			quit=true;
			break;
		}
	}
}

problem is when i try enter an employee(engg. manager or researcher)
it skips first name. And after entering data it stops. Debugger breaks at line 46 of main().cpp(where i gave new command)
Last edited on
Your problem is the mix of >> operator and getline()

Read this:
http://www.cplusplus.com/articles/S3wTURfi/
thanks for the article. Here is what i did:
i entered cin.ignore() after cin>>input line 32 main().cpp. Now its working fine. Now only problem with code is that after i enter the details it just stop. Am i making any mistake with new statement??
help anyone??
The problem is the variable j. It is not initialize. That means it has a random value which is most likely out of bounds.

I find it rather strange that you use a vector (database) and an array (emp) for the same pointers. Why?
ok i figured i didnt initialised j. its working fine now.Only two problems persists first printStats() fuction seems to be working for Manager class only. Rest of them are just printing fistName,lastName and salary. second problem is that the outFile is not storing complete details.
printStats() fuction seems to be working for Manager class only
Lines 53,54,55 of class Engineer hides the data on line 28,29,30 in class Employee. Just remove them

the outFile is not storing complete details
Save on line 38 does not overwrite save on line 19 because of the capital S
thanks for the reply. I made changes as pointed by you. outFile is now working. But no change in printing of stats.Its printing same thing.
printStats has the same problem as save.

PrintStats does not overwrite printStats due to the capital P. You need to make sure that both function are exactly the same. C++ is case sensitive
thanks man you are awesome. one last problem: after i try to search last name program just breaks and exits. Have i put break; at wrong place??
also enter last name just skips and exit the program.
no it's an unusual way to erase the entry, but other then that it looks ok.

you need to add a break; after line 97. Otherwise it goes thru to line 99.
alright i figured it out. in the search lastName snippet i replaced getline(cin,search) with cin>>search. That solved input problem.Secondly replaced break; out of braces just above case 3:
Now program is almost complete just one problem outFile is not working as intended it just print firstName,lastName and salary for first entry and everything for second entry.
Last edited on
in the search lastName snippet i replaced getline(cin,search) with cin>>search
That would mean that on deleting you can't enter spaces while on adding you can.
Put this
1
2
3
#include <limits>
...
cin.ignore(numeric_limits<streamsize>::max(), '\n');
before getline() in order to get rid of the previous new lines.


Secondly replaced break; out of braces
That means you remove all employees with that last name instead of just the first (like you did before). That doesn't sound right, don't you think so? In the real world this would be done by a unique number for each employee


Now program is almost complete just one problem outFile is not working as intended it just print firstName,lastName and salary for first entry and everything for second entry.
This is due to the aforementioned weird doubly pointer. In other words: you remove the employee from database but not from emp so they drift apart...
thanks for all the help you did. Only thing i did'nt understood here is
 
you remove the employee from database but not from emp so they drift apart... 

anyone.... how can i save different types of employees. I mean engineer,manager and researcher have different details. Then how am i suppose to save each from my database to outFile??
Remove line 13 and all adjacent accesses to emp. Try to avoid pointers to the same object in different containers. with database you can do whatever you want.

For example line 46,47,48 can be replaced by:
1
2
				database.push_back(new Engineer(firstName,lastName,sal,knowCPP,exp,trade));
				database.back()->save(outFile);

Then you don't have the problem with two arrays that diverge
okay if i remove emp then could you please tell me how can i use printStats()??
what i intend to do here is that i could ask user to save list of data to file.
Like so:
1
2
3
4
5
		for(int k=0;k<database.size();k++)
		{
			database[k]->printStats(); // Note!
			cout<<endl<<endl;
		}
you sir are of great help. However one problem still persists. I have added another option as
 
cout<<"1.add an employee 2.delete an employee 3. save 4.exit"<<endl;

i m using this as my case 3:
1
2
3
4
5
6
case 3:
			for(int i=0;i<database.size();i++)
			{
				database[i]->save(outFile);
				cout<<endl;
			}

Only problem is that as i delete an employee and save in database. database entry is saved 2 times. Here is my 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
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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
//main().cpp
#include<iostream>
#include<fstream>
#include<limits>
#include<vector>
#include<string>
#include"employee.h"
using namespace std;
int main()
{
	vector<Employee*> database;
	bool quit=false;
	ofstream outFile("out.txt");
	//Employee* emp[50];
	int input,select1,j=0,sal,vac,meet,exp;
	string firstName;string lastName;string school;string topic;string knowCPP;string trade;string search;
	while(!quit)
	{
		cout<<"Database size:"<<database.size()<<endl;
		cout<<"Database contains:"<<endl;
		for(int k=0;k<database.size();k++)
		{
			database[k]->printStats();
			cout<<endl<<endl;
		}
		
		cout<<"\n1.Add an employee 2.Delete an employee 3.save 4. Exit ";
		cin>>select1;
		cout<<endl;
		switch(select1)
		{
		case 1:
			cout<<"\n1.Add a manager 2. Add an engineer 3.Add a researcher ";
			cin>>input;
			cin.ignore();
			switch(input)
			{
			case 1:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cout<<"\nEnter number of meetings in an year:";
				cin>>meet;
				cout<<"\nEnter number of vacations in year:";
				cin>>vac;
				//emp[j]=new Manager(firstName,lastName,sal,meet,vac);
				database.push_back(new Manager(firstName,lastName,sal,meet,vac));
				//emp[j]->save(outFile);
				//j++;
				break;
			case 2:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cout<<"\nEnter whether know c++ y/n:";
				cin>>knowCPP;
				cout<<"\nEnter trade:";
				cin>>trade;
				cout<<"\nEnter years of experience:";
				cin>>exp;
				//emp[j]= new Engineer(firstName,lastName,sal,knowCPP,exp,trade);
				database.push_back(new Engineer(firstName,lastName,sal,knowCPP,exp,trade));
				//emp[j]->save(outFile);
				//j++;
				break;
			case 3:
				cout<<"\nEnter first name:";
				getline(cin,firstName);
				cout<<"\nEnter last name:";
				getline(cin,lastName);
				cout<<"\nEnter salary:";
				cin>>sal;
				cin.ignore();
				cout<<"\nEnter the name of school of Phd.:";
				getline(cin,school);
				cout<<"\nEnter the topic pf Phd.:";
				getline(cin,topic);
				//emp[j]=new Researcher(firstName,lastName,sal,school,topic);
				database.push_back(new Researcher(firstName,lastName,sal,school,topic));
				//emp[j]->save(outFile);
				//j++;
				break;
			}
			break;
		case 2:
			cout<<"enter last name of employee to be deleted:";
			cin.ignore(numeric_limits<streamsize>::max(),'\n');
			getline(cin,search);
			for(int l=0;l<database.size();l++)
			{
				if(database[l]->getLastName()==search)
				{
					database.erase(database.begin()+l);
					break;
				}
					
			}
		case 3:
			for(int i=0;i<database.size();i++)
			{
				database[i]->save(outFile);
				cout<<endl;
			}
			break;
		case 4:
			cout<<"\nExiting............";
			quit=true;
			break;
		}
	}
}

and thanks again for all the help you have been doing.
Pages: 12