Answer to Question #334204 in C++ for saen

Question #334204

Write a C++ program which takes multiple Employees information (records) as inputs from the


user and save them on a file using class. Writing in file should be done using setters. Perform


following operations on the data saved in the file:


• Read data from file


• Search an employee’s information in the file


• Count the number of employees having salary more than 50,000.

1
Expert's answer
2022-04-29T08:21:25-0400
#include <iostream>
#include <string>
#include <fstream>


using namespace std;


class Employee
{
	int emp_id;
	string emp_name;
	string emp_exper;
	double emp_salary;
public:
	Employee(){}
	void Assign()
	{
		cout << "Please, enter an id of employee: ";
		cin >> emp_id;
		cout << "Please, enter a name of employee: ";
		cin >> emp_name;
		cout << "Please, enter an employee experience: ";
		cin >> emp_exper;
		cout << "Please, enter a doc salary of employee: ";
		cin >> emp_salary;
	}
	int getId() { return emp_id; }
	string getName() { return emp_name; }
	string getExper() { return emp_exper; }
	double getSalary() { return emp_salary; }
};


void Menu()
{
	cout << "\nSelect some of the following: "
		<< "\n1 - Read file with employees"
		<< "\n2 - Search data"
		<< "\n3 - Count employees having salary more 50000"
		<< "\n0 - Exit program"
		<< "\nMake a select: ";
}


void Show()
{
	ifstream ifs("testRecords.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			cout << line << endl;
		}
		ifs.close();
	}
}


void Search()
{
	string input;
	cout << "Please, take input (ID, name, experience, salary): ";
	cin >> input;
	ifstream ifs("testRecords.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		bool found = false;
		while (getline(ifs, line, '\n'))
		{
			if (line.find(input) != string::npos)
			{
				cout << line << endl;
				found = true;
				break;
			}
		}
		if (found == false)
			cout << input << " is not found!";
		ifs.close();
	}
}


int CountEmpl()
{
	int count = 0;
	ifstream ifs("testRecords.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			line = line.substr(line.find(' ') + 1);
			line = line.substr(line.find(' ') + 1);
			line = line.substr(line.find(' ') + 1);
			if (stoi(line) > 50000)
				count++;
		}
		ifs.close();
	}
	return count;
}


int main()
{
	int n;
	cout << "Please, enter a number of employees records: ";
	cin >> n;
	Employee *arr = new Employee[n];
	for (int i = 0; i < n; i++)
	{
		arr[i].Assign();
	}
	ofstream of;
	of.open("testRecords.txt");
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < n; i++)
		{
			of << arr[i].getId() << " " << arr[i].getName()
				<< " " << arr[i].getExper() << " "
				<< arr[i].getSalary() << endl;
		}
	}
	of.close();
	char choice;
	do
	{
		Menu();
		cin >> choice;
		switch (choice)
		{
		case '1':
		{
			Show();
			break;
		}
		case '2':
		{
			Search();
			break;
		}
		case '3':
		{
			cout<<CountEmpl();
			break;
		}
		}
	} while (choice != '0');
}

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
APPROVED BY CLIENTS