In Microsoft Visual Studio C++ Hardware Inventory – Write a database to keep tra
ID: 3854598 • Letter: I
Question
In Microsoft Visual Studio 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.
Sample Program session
(First Run)
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
Record # Tool name Quantity Cost
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 1
Enter record number ( 1 to 100, 0 to return to main menu )
? 5
Enter tool name, quantity, cost
? saw 102 12
Enter record number ( 1 to 100, 0 to return to main menu )
? 7
Enter tool name, quantity, cost
? hammer 75 8
Enter record number ( 1 to 100, 0 to return to main menu )
? 0
Enter request
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
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
1 - Input new tool or update an existing tool
2 - Delete a tool
3 - List all tools
4 - Exit
? 3
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
Explanation / Answer
#include <iostream>
#include <fstream>
#include <cstring>
#include <iomanip>
using namespace std;
struct HARDWARE_ITEM{
int record_num = 0;
char tool_name[100];
int quantity = 0;
float cost =0.0;
bool is_active = 0;
};
HARDWARE_ITEM records[99];
void CreateEmptyDatabase(void)
{
int rec_num = 0;
for(int i=0;i<100; i++)
{
rec_num ++;
records[i].record_num = rec_num;
//fs.write( (char*)&records[i], sizeof(HARDWARE_ITEM) );
}
fstream fs("hardware.dat", fstream::out|fstream::app|fstream::binary );
fs.write( (char*)&records, sizeof(HARDWARE_ITEM)*100 );
fs.close();
}
void AddUpdateRecord()
{
fstream fs("hardware.dat", fstream::in|fstream::out|fstream::binary );
fs.write( (char*)&records, sizeof(HARDWARE_ITEM)*100 );
fs.close();
}
void ListItems()
{
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 );
//fs.open("hardware.dat", fstream::in | fstream::binary | fstream::ate);
HARDWARE_ITEM item;
int items_count = 0;
while( fs.read( (char*)&item, sizeof(HARDWARE_ITEM)) )
{
//items_count = items_count + 1;
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 <<" ";
}
}
//cout << endl << "Total items :" << items_count << endl;
fs.close();
}
int main()
{
fstream dbfile;
dbfile.open("hardware.dat", fstream::in);
if (!dbfile.is_open())
{
CreateEmptyDatabase();
}
else
{
dbfile.read( (char*)&records, sizeof(HARDWARE_ITEM)*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;
AddUpdateRecord();
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;
AddUpdateRecord();
break;
case 3: // List all tools
ListItems();
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.