Answer to Question #334901 in C++ for nitish

Question #334901

Write a program to take input for n number of employee records and write records of all employees in a file named: “record1”. Also write records of all those employees in another file named: “record2” who are taking salary more than 50,000. After writing records in both files, merge their contents in another file: “finalrecord” and read all records of “finalrecord” file and display on screen. [Attributes of employee: emp_id, emp_name, emp_experience, emp_salary]


1
Expert's answer
2022-04-28T13:21:35-0400
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

struct Employee
{
	int emp_id;
	string emp_name;
	string emp_exper;
	double emp_salary;
};

int main()
{
	vector<Employee>vec;
	int n;
	cout << "Please, enter a number of doctor records: ";
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		Employee e;
		cout << "Please, enter an id of employee: ";
		cin >> e.emp_id;
		cout << "Please, enter a name of employee: ";
		cin >> e.emp_name;
		cout << "Please, enter an employee experience: ";
		cin >> e.emp_exper;
		cout << "Please, enter a doc salary of employee: ";
		cin >> e.emp_salary;
		vec.push_back(e);
	}
	ofstream of;
	of.open("record1.txt");
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < n; i++)
		{
			of << vec[i].emp_id << " " << vec[i].emp_name
					<< " " << vec[i].emp_exper << " "
					<< vec[i].emp_salary << endl;
		}
	}
	of.close();
	ofstream of2;
	of2.open("record2.txt");
	if (!of2.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < n; i++)
		{
			if (vec[i].emp_salary > 50000)
			{
				of2 << vec[i].emp_id << " " << vec[i].emp_name
					<< " " << vec[i].emp_exper << " "
					<< vec[i].emp_salary << endl;
			}
		}
	}
	of2.close();
	ifstream file1("record1.txt");
	ifstream file2("record2.txt");
	ofstream out("finalrecord.txt");
	if (!file1.is_open() || !file2.is_open() || !out.is_open())
	{
		cout << "File isn`t opened!";
	}
	else
	{
		string str;
		while (getline(file1, str)) { out << str << endl; }
		while (getline(file2, str)) { out << str << endl; }
	}
	out.close();
	ifstream res("finalrecord.txt");
	if (!res.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(res, line))
		{
			cout << line << endl;
		}
	}
}

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