Need a working program in C++. Hardware Inventory - Write a database to keep tra
ID: 3765362 • Letter: N
Question
Need a working program in C++.
Hardware Inventory - Write a database to keep track of tools, their cost and number. Your program should initialize hardware.dat to 100 empty records, let the user input a record number, tool name, cost and number of that tool. Your program should let you delete and edit records in the database. The next run of the program must start with the data from the last session.
Example Program Session: (First Run)Enter request
4 - Exit ?3
?1
Enter record number ?5
( 1 to 100, 0 to return to main menu )
Enter tool name, quantity, cost
? saw 102 12
Enter record number ( 1 to 100, 0 to return to main menu ) ?7
Enter record number ?0
Enter request
?3
( 1 to 100, 0 to return to main menu )
Record # Tool name Quantity Cost 5 saw 102 12.00 7 hammer 75 8.00 Enter request
1 - Input new tool or update an existing tool 2 - Delete a tool
3 - List all tools
4 - Exit
?4
Press any key to continue . . . (Second Run)Enter request
?3
Record # Tool name 5 saw
7 hammer Enter request
or update
?4
Press any key to continue ...
or update
Explanation / Answer
working c++ code compiled on ideone
I have created a hardware store with the required struct variables
There are 3 functions in the code:
create_database:
to read the hardware file and to update the required hardware struct
add_record:
to add a new record to the structure
list_record:
to show the information of any item in the store:
c++ code:
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
struct Hardware_Store{
int record_num = 0;
char tool_name[100];
int quantity = 0;
float cost =0.0;
bool is_active = 0;
};
Hardware_Store records[99];
void create_database(void)
{
int rec_num = 0;
for(int i=0;i<100; i++)
{
rec_num ++;
records[i].record_num = rec_num;
}
fstream fs("hardware.dat", fstream::out|fstream::app|fstream::binary );
fs.write( (char*)&records, sizeof(Hardware_Store)*100 );
fs.close();
}
void add_record()
{
fstream fs("hardware.dat", fstream::in|fstream::out|fstream::binary );
fs.write( (char*)&records, sizeof(Hardware_Store)*100 );
fs.close();
}
void list_record()
{
cout << setw(10) << left << "Record #"
<< setw(35) << left << "Tool name"
<< setw(12) << left << "Quantity"
<< setw(5) << left << "Cost" <<" ";
cout << setw(10) << left << "---------"
<< setw(35) << left << "-------------------------------"
<< setw(12) << left << "----------"
<< setw(5) << left << "-----" <<" ";
fstream fs("hardware.dat", fstream::in|fstream::binary );
Hardware_Store item;
int items_count = 0;
while( fs.read( (char*)&item, sizeof(Hardware_Store)) )
{
if (item.is_active == 1)
{
cout << setw(10) << left << item.record_num
<< setw(35) << left << item.tool_name
<< setw(12) << left << item.quantity
<< setw(5) << left << item.cost <<" ";
}
}
fs.close();
}
int main()
{
fstream dbfile;
dbfile.open("hardware.dat", fstream::in);
if (!dbfile.is_open())
{
create_database();
}
else
{
dbfile.read( (char*)&records, sizeof(Hardware_Store)*100 );
dbfile.close();
}
int option = 0;
while(1)
{
cout << " Enter Request ";
cout << "1 - Input new tool or update an existing tool ";
cout << "2 - Delete a tool ";
cout << "3 - List all tools ";
cout << "4 - Exit ";
cout << "? ";
cin >> option;
int rec_num;
switch (option)
{
case 1: // Input new tool or update an existing tool
cout << "Enter record number ( 1 to 100, 0 to return to main menu )" << endl << "? ";
cin >> rec_num;
if (rec_num == 0)
{
break;
}
cout << "Enter tool name, quantity, cost " << endl << "? ";
cin >> records[rec_num-1].tool_name;
cin >> records[rec_num-1].quantity;
cin >> records[rec_num-1].cost;
records[rec_num-1].is_active = 1;
add_record();
break;
case 2: // Delete a tool
cout << "Enter record number ( 1 to 100, 0 to return to main menu )" << endl << "? ";
cin >> rec_num;
if (rec_num == 0)
{
break;
}
strcpy(records[rec_num-1].tool_name, "");
records[rec_num-1].quantity = 0;
records[rec_num-1].cost = 0;
records[rec_num-1].is_active = 0;
add_record();
break;
case 3: // List all tools
list_record();
break;
case 4: // Exit.
return 0;
break;
}
}
return 0;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.