Answer to Question #328309 in C++ for Ryuk

Question #328309

Q1: Write a program to take input for n number of doctor records and write records of all cardiologists in a file named: “record1”. Also write records of all those doctors in another file named: “record2” who are taking salary more than INR 80,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 doctor: doc_id, doc_name, doc_specialization, doc_salary]

1
Expert's answer
2022-04-13T09:01:21-0400
#include <iostream>
#include <string>
#include <vector>
#include <fstream>

using namespace std;

struct Doctor
{
	int doc_id;
	string doc_name;
	string doc_specialization;
	double doc_salary;
};

int main()
{
	vector<Doctor>vec;
	int n;
	cout << "Please, enter a number of doctor records: ";
	cin >> n;
	for (int i = 0; i < n; i++)
	{
		Doctor d;
		cout << "Please, enter an id of doctor: ";
		cin >> d.doc_id;
		cout << "Please, enter a name of doctor: ";
		cin >> d.doc_name;
		cout << "Please, enter a doc specialization of doctor: ";
		cin >> d.doc_specialization;
		cout << "Please, enter a doc salary of doctor: ";
		cin >> d.doc_salary;
		vec.push_back(d);
	}
	ofstream of;
	of.open("record1.txt");
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < n; i++)
		{
			if (vec[i].doc_specialization == "cardiologist")
			{
				of << vec[i].doc_id << " " << vec[i].doc_name
					<< " " << vec[i].doc_specialization << " "
					<< vec[i].doc_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].doc_salary > 80000)
			{
				of2 << vec[i].doc_id << " " << vec[i].doc_name
					<< " " << vec[i].doc_specialization << " "
					<< vec[i].doc_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