Chapter 11: Hardware Inventory %u2013 Write a database to keep track of tools, t
ID: 3538221 • Letter: C
Question
Chapter 11:
Hardware Inventory %u2013 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
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 %u2026
Explanation / Answer
#include <stdio.h>
#include <math.h>
struct hardwareData
{
int recordNum;
char toolname[20];
int quantity;
double cost;
};
/* prototypes */
int enterChoice( void );
void textFile( FILE *readPtr );
void updateRecord( FILE *fPtr );
void newRecord( FILE *fPtr );
void deleteRecord( FILE *fPtr );
FILE *hfPtr;
int main()
{
FILE *cfPtr;
int choice;
if ((cfPtr = fopen( "hardware.dat", "rb+" )) == NULL )
{
printf( "File could not be opened. " );
}
else
{
while (( choice = enterChoice()) != 5 )
{
switch ( choice )
{
/* create text file from record */
case 1:
textFile( cfPtr );
break;
/* update record */
case 2:
updateRecord( cfPtr );
break;
/* create record */
case 3:
newRecord ( cfPtr );
break;
/* delete existing record */
case 4:
deleteRecord ( cfPtr );
break;
/* display message if user does not enter valid choice */
default:
printf( "Incorrect choice " );
break;
}
}
fclose( cfPtr );
}
return 0;
}
void textFile( FILE *readPtr )
{
FILE *writePtr;
struct hardwareData hardware = { 0, "", 0, 0.0 };
if ((writePtr = fopen( "hardware.txt", "w" )) == NULL )
{
printf( "File could not be opened. " );
}
else
{
rewind( readPtr );
fprintf( writePtr, "%-6s%-16s%-11s%-10s ","Record #", "Tool name", "Quantity", "Cost" );
/* copy all records from random-access file to text file */
while ( !feof( readPtr ) )
{
fread( &hardware, sizeof( struct hardwareData ), 1, readPtr );
/* write single record to text file */
if ( hardware.recordNum != 0 )
{
fprintf(writePtr, "%-6d%-16s%-11d%10.2lf ", hardware.recordNum, hardware.toolname, hardware.quantity, hardware.cost );
}
}
fclose( writePtr );
}
}
void updateRecord ( FILE *fPtr )
{
int record;
struct hardwareData hardware = { 0, "", 0, 0.0 };
printf( "Enter record to update (1-100): ");
scanf( "%d", &record );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fread( &record, sizeof( struct hardwareData ), 1, fPtr );
if ( hardware.recordNum == 0)
{
printf( "Record #%d has no information. ", record );
}
else
{ /* update record */
printf( "Enter new tool name, quantity,cost ?" );
scanf( "%s%d%lf", hardware.toolname, &hardware.quantity, &hardware.cost );
fseek( fPtr, ( record - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
/* write updated record over old record */
fwrite( &hardware, sizeof( struct hardwareData ), 1, fPtr );
}
}
void deleteRecord( FILE *fPtr )
{
struct hardwareData hardware;
struct hardwareData blankRecord = { 0, "", 0,0.0 };
int recordNum;
printf( "Enter record number to delete (1-100): " );
scanf( "%d", &recordNum );
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
if ( hardware.recordNum==0 )
{
printf ( "Record %d does not exist. ", recordNum );
}
else
{
fseek( fPtr, ( recordNum - 1 ) * sizeof( struct hardwareData ), SEEK_SET );
fwrite( &blankRecord, sizeof( struct hardwareData ), 1, fPtr );
}
}
void newRecord( FILE *fPtr )
{
struct hardwareData hardware = { 0, "", 0, 0.0 };
int piece;
printf( "Enter record number to create (1-100) : " );
scanf( "%d", &piece);
fseek( fPtr, (piece - 1) * sizeof( struct hardwareData ), SEEK_SET );
fread( &hardware, sizeof( struct hardwareData ), 1, fPtr );
if ( hardware.recordNum != 0 )
{
printf( "Record already exists. ", hardware.recordNum );
}
else
{
printf( "Enter tool name, quantity, cost ?" );
scanf("%s%d%.2lf", hardware.toolname, &hardware.quantity, &hardware.cost);
hardware.recordNum = piece;
fseek(fPtr, (hardware.recordNum-1) * sizeof( struct hardwareData), SEEK_SET);
fwrite( &hardware, sizeof( struct hardwareData), 1, hfPtr);
}
}
int enterChoice( void )
{
int menuChoice;
printf( " Enter your choice "
"1 - List of all tools "
"2 - Update an existing tool "
"3 - Add a new tool "
"4 - Delete a tool "
"5 - End program ? " );
scanf( "%d", &menuChoice );
return menuChoice;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.