Answer to Question #307996 in C++ for lko

Question #307996

Create an employee management system that contains the following detail of an employee. a) Employee ID b) Employee Name c) Employee Salary d) Job experience in terms of years Following operations can perform by using the linked list. 1) Insert the employee record 2) Delete the employee record 3) Search the employee record


1
Expert's answer
2022-03-08T16:03:48-0500

Here is what I did:

#include <iostream>
#include <fstream>
#include <vector>
#include <string>


using namespace std;


int main()
{
	int c ,savedata , enter1 ,enter2 , i = 0 , save = 0 , deletedata ,e =0;
	const int size = 10;
	int ID = 0;
	string bd = "BD.txt"; // Add a file that will store the names of employee data files
	fstream fs;
	fstream fe;
	string str, ster;
	string msg, filename;
	char ch, ce;
	vector<string> data = { "" };
	string EmployeeName;
	string EmployeeSalary;
	string JobExperience;
	fs.open(bd, fstream::in | fstream::out | fstream::app);
	cout << "What do you want to do?" << endl;
	cout << "1) Insert the employee record" << endl;
	cout << "2) Delete the employee record" << endl;
	cout << "3) Search the employee record" << endl;
	cin >> c;
	switch (c)
	{
	case 1:
		cout << "Enter Employee name: " << endl;
		cin >> EmployeeName;
		cout << "Enter Employee salary: " << endl;
		cin >> EmployeeSalary;
	    cout << "Enter job experience in terms of years: " << endl;
		cin >> JobExperience;
		ID++;
		fs << EmployeeName << "\n";
		fs.open(EmployeeName , fstream::in | fstream::out | fstream::app);
		fe.open(EmployeeName, fstream::in | fstream::out | fstream::app);
		fe << "Eployee ID: " << ID << endl;
		fe << "Employee name: " << EmployeeName << endl;
		fe << "Employee salary: " << EmployeeSalary << endl;
		fe << "Job experience: " << JobExperience << endl;
		break;
	case 2:
		cout << "" << endl;
		if (fs.peek() == EOF)
		{
			cout << "File BD empty" << endl;
			fs.close();
		
		}
		else
		{
			cout << "Change your savedata employee file for delete: " << endl;


			while (!fs.eof())
			{


				data[i] = (char*)malloc(sizeof(char) * save);
				fs >> data[i] >> ch;


				if (i > 0)
				{
					str = ce + data[i];
					data[i] = str;
				}
				ce = ch;
				cout << data[i] << endl;


				i++;
				if (i > 0)
				{
					data.push_back("");
				}
			}
			cin >> deletedata;


			if (remove(data[deletedata].c_str()) != -1)
			{
				cout << "File " << data[deletedata] << " Deleted" << endl;
				data.erase(data.begin() + deletedata);


				fs.close();
				if (remove(bd.c_str()) != -1)
				{
					fs.open(bd, fstream::in | fstream::out | fstream::app);
					for (; e < i; e++)
					{
						fs << data[e] << endl;
					}
				}

			}
			else
			{
				cout << "Error!" << endl;
			}


			fs.close();
		
		}
		break;
	case 3:
		if (fs.peek() == EOF)
		{
			cout << "File BD empty" << endl;
			
			cin >> enter1;
			fs.close();
		}
		else
		{
			cout << "Enter employee name: " << endl;
			cout << "Searchable names: " << endl;
			while (!fs.eof())
			{

				data[i] = (char*)malloc(sizeof(char) * size);
				fs >> data[i] >> ch;
				if (i > 0)
				{
					ster = ch + data[i];
					data[i] = ster;
				}
				ce = ch;
				cout << data[i] << "\0" << endl;
				i++;
				if (i > 0)
				{
					data.push_back("");
				}
			}
			cin >> filename;
		}
			fe.open(filename);


			while (fe.peek() == EOF)
			{
				cout << "File " << filename << "empty or does not exist!" << endl;
			}
			while (!fe.eof())
			{
				msg = "";
				getline(fe, msg);


				cout << msg << endl;
			}
			fe.close();
			fs.close();
		break;
	default:
		cout << "Error! Number entered incorrectly! " << endl;
		cin >> c;
		break;
	}

}

Of course it looks quite primitive, but this should be enough. A while loop could be added here that allowed us to repeat operations with employees until we were satisfied with the result.


Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog