C++ Write a database to keep track of tools, their cost and number. Your program
ID: 3764670 • Letter: C
Question
C++
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
include #include #include 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; void newRecord( FILE *fPtr ) { struct hardwareData hardware = { 0, "", 0, 0.0 }; int piece; FILE *hfPtr = fopen("hardware.txt","rb+"); 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); } }Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.