Answer to Question #323475 in C++ for think

Question #323475

using dev c++ please

(Hardware Inventory) You are the owner of the hardware store and need to keep an inventory that can tell you what different tools you have, how many of each you have on hand and the cost of each one. Write a program that lets you input the data concerning each tool, enables you to list all your tools, lets you delete a record for a tool that you no longer have and lets you update any information in the file. The tool identification number should be the record number. Use the following information to start your file.


RECORD#

TOOL NAME

QUANTITY

COST

3

Electric Sander

7

57.98

17

Hammer

76

11.99

24

Jig Saw

21

11.00

39

Lawn Mower

3

79.50

56

Power Saw

18

99.99

68

Screwdriver

106

6.99

77

Sledge Hammer

11

21.50

83

Wrench

34

7.50



1
Expert's answer
2022-04-04T16:11:39-0400
#include<iostream>
#include<string>
#include<map>
#include<fstream>

using namespace std;

void Menu()
{
	cout << "\nChoose from the following:"
		<< "\nA - Add new record"
		<< "\nS - Show the all record"
		<< "\nD - Delete the record"
		<< "\nU - Update the record"
		<< "\nF - Update data in file"
		<< "\nE - Exit program";
	cout << "\nMake a select: ";
}

struct Record
{
	string toolName;
	int quantity;
	float cost;
};

void AddRecord(map<int, Record>& m)
{
	int num;
	Record r;
	cout << "Enter a number of the record: ";
	cin >> num;
	cout << "Enter a tool Name: ";
	cin >> r.toolName;
	cout << "Enter a quantity: ";
	cin >> r.quantity;
	cout << "Enter a cost: ";
	cin >> r.cost;
	m[num] = r;
}

void ShowRecords(map<int, Record>& m)
{
	map<int, Record>::iterator it;
	for (it = m.begin(); it != m.end(); ++it)
	{
		cout << it->first << " " << it->second.toolName << " "
			<< it->second.quantity << " " << it->second.cost << endl;
	}
}

void DeleteRecord(map<int, Record>& m)
{
	int n;
	cout << "Please, enter a number of record: ";
	cin >> n;
	m.erase(n);
}

void UpdateRecord(map<int, Record>& m)
{
	int n;
	cout << "Please, enter a number of record to update: ";
	cin >> n;
	cout << "Enter a tool Name: ";
	cin >> m[n].toolName;
	cout << "Enter a quantity: ";
	cin >> m[n].quantity;
	cout << "Enter a cost: ";
	cin >> m[n].cost;
}

void UpdateFile(map<int, Record>& m)
{
	ofstream of;
	of.open("Inventory.txt",ofstream::out| ofstream::trunc);
	if (!of.is_open())
		cout << "File isn`t opened!";
	else
	{
		map<int, Record>::iterator it;
		for (it = m.begin(); it != m.end(); ++it)
		{
			of << it->first << " " << it->second.toolName << " "
				<< it->second.quantity << " " << it->second.cost << endl;
		}
	}
	of.close();
}

int main()
{
	map<int, Record>m;
	ifstream ifs("Inventory.txt");
	if (!ifs.is_open())
		cout << "File isn`t opened!";
	else
	{
		string line;
		while (getline(ifs, line, '\n'))
		{
			string str = line.substr(0, line.find(' '));
			int num = stoi(str);
			Record r;
			line = line.substr(line.find(' ') + 1);
			str = line.substr(0, line.find(' '));
			r.toolName=str;
			line = line.substr(line.find(' ') + 1);
			str = line.substr(0, line.find(' '));
			r.quantity = stoi(str);
			line = line.substr(line.find(' ') + 1);
			str = line.substr(0, line.find(' '));
			r.cost= stod(str);
			m[num] = r;
		}
		ifs.close();
	}
	
	char choice;
	do
	{
		Menu();
		cin >> choice;
		switch (choice)
		{
			case 'A':case 'a':
			{
				AddRecord(m);
				break;
			}
			case 'S':case 's':
			{
				ShowRecords(m);
				break;
			}
			case 'D':case 'd':
			{
				DeleteRecord(m);
				break;
			}
			case 'U':case 'u':
			{
				UpdateRecord(m);
				break;
			}
			case 'F':case 'f':
			{
				UpdateFile(m);
				break;
			}
		}
	} while (choice != 'E');
}

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