employee database problem

  • (3 Pages)
  • +
  • 1
  • 2
  • 3

35 Replies - 1894 Views - Last Post: 22 August 2012 - 09:40 AM Rate Topic: -----

#16 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 20 August 2012 - 07:31 PM

could you elaborate a little. if i close outfile with outFile.close(). Then how can i resave all employees after reopening it??
Was This Post Helpful? 0
  • +
  • -

#17 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1899
  • View blog
  • Posts: 5,696
  • Joined: 05-May 12

Re: employee database problem

Posted 20 August 2012 - 07:35 PM

You have the for loop that tries to find the employee to delete. If you are not deleting an employee, you should be calling Employee::save() to save it back out to the file.
Was This Post Helpful? 0
  • +
  • -

#18 jimblumberg  Icon User is online

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,280
  • Joined: 25-December 09

Re: employee database problem

Posted 20 August 2012 - 07:37 PM

You will need to re-write the information you want to save. You can't just "erase" a record in your file, you need to insure you have all the information from the file in memory, erase the file, then re-write the information back into the file.

Jim
Was This Post Helpful? 0
  • +
  • -

#19 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 20 August 2012 - 07:40 PM

okay i got that. Problem i have here is that how should i call Employee::save(). I mean do i have to make emp[l] then save out all the data??

This post has been edited by cniper: 20 August 2012 - 07:41 PM

Was This Post Helpful? 0
  • +
  • -

#20 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 20 August 2012 - 07:54 PM

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??
Was This Post Helpful? 0
  • +
  • -

#21 jimblumberg  Icon User is online

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,280
  • Joined: 25-December 09

Re: employee database problem

Posted 20 August 2012 - 08:32 PM

You are already saving information to your output file. However every time you open that file you erase the data. Is that really what you want? If not then you will need to open the file without erasing the information that is currently in the file. To do this you will need to use the second parameter of the ofstream constructor. Usually you would use the ape open mode. Once you are able to open the file for writing without erasing the contents you will need to create a function that can read your file contents into your vector.

You may want to study this link for basic file input and output.

Jim
Was This Post Helpful? 0
  • +
  • -

#22 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1899
  • View blog
  • Posts: 5,696
  • Joined: 05-May 12

Re: employee database problem

Posted 20 August 2012 - 08:40 PM

View Postcniper, on 20 August 2012 - 07:40 PM, said:

okay i got that. Problem i have here is that how should i call Employee::save(). I mean do i have to make emp[l] then save out all the data??


View Postcniper, on 20 August 2012 - 07:54 PM, said:

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??


That was the entire point of the polymorphism... all you need to do is call emp[i]->save(outFile);. Each employee type should know how to save itself.
Was This Post Helpful? 0
  • +
  • -

#23 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: employee database problem

Posted 20 August 2012 - 08:53 PM

Usually the data from the file would be read into a container/array when the program started. While it is in memory the list of employees can be modified. When the program finishes the user can be asked if they wish to save the list of data to the file.

To identify each type of employee when reading the file, a code letter could be used at the beginning of the record. Eg M for manager.
Was This Post Helpful? 1
  • +
  • -

#24 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 21 August 2012 - 04:32 AM

thats what i really wanna do. Is it possible that i use database directly without using emp??
Was This Post Helpful? 0
  • +
  • -

#25 jimblumberg  Icon User is online

  • member icon

Reputation: 3044
  • View blog
  • Posts: 9,280
  • Joined: 25-December 09

Re: employee database problem

Posted 21 August 2012 - 06:42 AM

Quote

Is it possible that i use database directly without using emp??

Your database is your Employee class. Your problem is that you need to be able to read the data from your file into your different inherited classes. In order to do this, since each of your inherited classes are different, you need a way of telling to which of your inherited classes this record belongs. You can do this by adding another "field" to your file to indicate to which class this data belongs. Another way would be to place each of your "classes" into their own file, for example the Managers have their own file, as do Engineers. For simplicity I would recommend the first method. Also I would suggest that instead of placing each "field" on a separate line that you place each "record" on it's own line separated by commas. For example for a Manager you have the following information stored in your file as:

FirstName
LastName
salary
mMeet
mVac


I recommend you store your information as
Manager,FirstName,LastName,salary,mMeet,mVac

So when you add another employee you would then end up with something like:
Manager,FirstName,LastName,salary,mMeet,mVac
Engineer,FirstName,LastName,salary,knowCPP,yearsExp,trade


In my opinion this will make reading your file much easier because now you can read one line of text that will contain all the relevant information to allow proper construction of the proper class instance. You would read this line into a string and then parse the first field so you can tell what type of employee this record contains, then call a function that properly parses that type of employee. This would require a function that knows how to parse a string containing the correct information for that type of employee.

Jim
Was This Post Helpful? 1
  • +
  • -

#26 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 21 August 2012 - 03:35 PM

thank you for all of your suggestions. Here is something else i tried to do:
//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;
		}
	}
}


Only problem is that as i delete an employee and save in database. database entry is saved 2 times.
Was This Post Helpful? 0
  • +
  • -

#27 #define  Icon User is offline

  • Duke of Err
  • member icon

Reputation: 974
  • View blog
  • Posts: 3,390
  • Joined: 19-February 09

Re: employee database problem

Posted 21 August 2012 - 03:51 PM

Hi. You don't have a break statement at the end of case 2 (delete). So case 3 will be processed as well.
Was This Post Helpful? 1
  • +
  • -

#28 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 21 August 2012 - 04:14 PM

oh gr8!!! it works fine now. thank you for all your help!!

This post has been edited by cniper: 21 August 2012 - 04:17 PM

Was This Post Helpful? 1
  • +
  • -

#29 Skydiver  Icon User is online

  • Code herder
  • member icon

Reputation: 1899
  • View blog
  • Posts: 5,696
  • Joined: 05-May 12

Re: employee database problem

Posted 21 August 2012 - 05:01 PM

Have you considered what happens when the user chooses option 3 two or more times?
Was This Post Helpful? 1
  • +
  • -

#30 cniper  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 43
  • Joined: 02-July 12

Re: employee database problem

Posted 21 August 2012 - 05:49 PM

no really!!! thanks for pointing it out. any solution to this problem.
Was This Post Helpful? 0
  • +
  • -

  • (3 Pages)
  • +
  • 1
  • 2
  • 3