Answer to Question #327191 in C++ for Abhi

Question #327191

Write a program to write 5 book records in a file. Program should also perform random access in the file, after taking input for a particular record number to read and it should also perform random record overwriting after taking input of new record and the record number to overwrite. [Attributes of book: Book_id, Book_price, Book_name, Book_author_name]


1
Expert's answer
2022-04-11T07:49:59-0400
#include<iostream>
#include<string>
#include<fstream>


using namespace std;


struct Book
{
	int Book_id;
	float Book_price;
	string Book_name;
	string Book_author_name;
};


void Menu()
{
	cout << "\nSelect from the following: "
		<< "\n1 - Find the record"
		<< "\n2 - Edit the record"
		<< "\n3 - Add new record"
		<< "\n4 - Show all records"
		<< "\n0 - Exit program";
	cout << "\nYour select: ";
}


void FindRecord()
{
	int Bid;
	cout << "\nPlease, enter a book id to read record from file: ";
	cin >> Bid;
	bool found = false;
	ifstream ifs("record.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			if (line.find(to_string(Bid)) != string::npos)
			{
				cout << line << endl;
				found = true;
			}
		}
	}
	if (!found)cout << "\nThe record wasn`t found";
	ifs.close();
}


void EditRecord()
{
	int Bid;
	cout << "\nPlease, enter a book id to read record from file: ";
	cin >> Bid;
	bool found = false;
	fstream fs("record.txt", ios::in | ios::out);
	if (!fs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(fs, line, '\n'))
		{
			if (line.find(to_string(Bid)) != string::npos)
			{
				cout << line << endl;
				found = true;
				fs.seekg(-1, ios::cur);
				Book tmp;
				cout << "Please, enter a book id: ";
				cin >> tmp.Book_id;
				fs << tmp.Book_id << " ";
				cout << "Please, enter a book price: ";
				cin >> tmp.Book_price;
				fs << tmp.Book_price << " ";
				cout << "Please, enter a book name: ";
				cin >> tmp.Book_name;
				fs << tmp.Book_name << " ";
				cout << "Please, enter a book author name: ";
				cin >> tmp.Book_author_name;
				fs << tmp.Book_author_name;
				break;
			}
		}
	}
	if (!found)cout << "\nThe record wasn`t found";
	fs.close();
}


void AddRecord()
{
	fstream fs("record.txt", ios::out | ios::app);
	if (!fs.is_open())
		cout << "File isn`t opened!";
	else
	{
		Book tmp;
		cout << "Please, enter a book id: ";
		cin >> tmp.Book_id;
		fs << tmp.Book_id << " ";
		cout << "Please, enter a book price: ";
		cin >> tmp.Book_price;
		fs << tmp.Book_price << " ";
		cout << "Please, enter a book name: ";
		cin >> tmp.Book_name;
		fs << tmp.Book_name << " ";
		cout << "Please, enter a book author name: ";
		cin >> tmp.Book_author_name;
		fs << tmp.Book_author_name;
	}
	fs.close();
}


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


int main()
{
	Book arr[5];
	for (int i = 0; i < 5; i++)
	{
		cout << "Please, enter a book id: ";
		cin >> arr[i].Book_id;
		cout << "Please, enter a book price: ";
		cin >> arr[i].Book_price;
		cout << "Please, enter a book name: ";
		cin >> arr[i].Book_name;
		cout << "Please, enter a book author name: ";
		cin >> arr[i].Book_author_name;
	}
	ofstream of;
	of.open("record.txt");
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		for (int i = 0; i < 5; i++)
		{
			if (arr[i].Book_price < 500)
			{
				of << arr[i].Book_id << " " << arr[i].Book_price << " "
					<< arr[i].Book_name << " " << arr[i].Book_author_name << endl;
			}
		}
	}
	of.close();
	char select;
	do
	{
		Menu();
		cin >> select;
		switch (select)
		{
		case '1':
		{
			FindRecord();
			break;
		}
		case '2':
		{
			EditRecord();
			break;
		}
		case '3':
		{
			AddRecord();
			break;
		}
		case '4':
		{
			ShowRecords();
			break;
		}
		}
	} while (select != '0');


	delete[]arr;
}

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