Title: Assignment
1Assignment 11
2Problem 14.12
3Flow Chart
start
Create file
Input data
Delete file
choose file
update file
choose file
end
4PSEUDOCODE
-
- Level 1.0
- Input
- Process
- Output
-
- Level 2.0
- Input
-
- Create file
- Input data
-
- Process
- If
- Delete file
- Then
- Choose file to delete
- Else
- Go to update file
5Source Code Page 1
//Solution to excercise 14.12 include
ltiostream.hgt include ltfstream.hgt include
ltiomanip.hgt include ltstring.hgt include
ltctype.hgt include ltstdlib.hgt void
initializeFile( fstream ) void inputData(
fstream ) void listTools( fstream ) void
updateRecord( fstream ) void insertRecord(
fstream ) void deleteRecord( fstream ) int
instructions( void ) const int LENGTH
30 struct Data int partNumber char
toolName LENGTH int inStock double
unitPrice int main() int choice
char response fstream file( "hardware.dat",
iosin iosout ) void ( f )( fstream
) listTools, updateRecord, insertRecord,
deleteRecord
- if ( !file )
- cerr ltlt "File could not be opened.\n"
- exit( EXIT_FAILURE )
-
- cout ltlt "Should the file be initialized (Y or
N) " - cin gtgt response
- while ( toupper( response ) ! 'Y' toupper(
response ) ! 'N' ) - cout ltlt "Invalid response. Enter Y or N "
- cin gtgt response
-
- if ( toupper( response ) 'Y' )
- initializeFile( file )
- inputData( file )
-
- while ( ( choice instructions() ) ! 5 )
- ( f choice - 1 )( file )
6Source Code Page 2
- Data blankItem -1, "", 0, 0.0
- // See Chapter 21 for a discussion of
reinterpret_cast - for ( int i 0 i lt 100 i )
- fRef.write( reinterpret_castlt char gt(
blankItem ), sizeof( Data ) ) -
- void inputData( fstream fRef )
-
- Data temp
- cout ltlt "Enter the partnumber (0 - 99, -1 to
end input) " - cin gtgt temp.partNumber
- while ( temp.partNumber ! -1 )
- cout ltlt "Enter the tool name "
- cin.ignore() // ignore the newline on the
input stream - cin.get( temp.toolName, LENGTH )
- cout ltlt "Enter quantity and price "
- cout ltlt "Enter the partnumber (0 - 99, -1 to end
input) " - cin gtgt temp.partNumber
-
-
- int instructions( void )
-
- int choice
- cout ltlt "\nEnter a choice\n1 List all
tools." - ltlt "\n2 Update record.\n3 Insert
record." - ltlt "\n4 Delete record.\n5 End
program.\n" - do
- cout ltlt "? "
- cin gtgt choice
- while ( choice lt 1 choice gt 5 )
- return choice
7Source Code Page 3
- cout ltlt setw( 7 ) ltlt "Record" ltlt " " ltlt
setiosflags( iosleft ) - ltlt setw( 30 ) ltlt "Tool name" ltlt
resetiosflags( iosleft ) - ltlt setw( 13 ) ltlt "Quantity" ltlt setw( 10 )
ltlt "Cost\n" - for ( int count 0 count lt 100
!fRef.eof() count ) - fRef.seekg( count sizeof( Data ) )
- fRef.read( reinterpret_castlt char gt(
temp ), sizeof( Data ) ) - if ( temp.partNumber gt 0
temp.partNumber lt 100 ) - cout.setf( iosfixed iosshowpoint
) - cout ltlt setw( 7 ) ltlt temp.partNumber ltlt
" " - ltlt setiosflags( iosleft ) ltlt
setw( 30 ) ltlt temp.toolName - ltlt resetiosflags( iosleft ) ltlt
setw( 13 ) ltlt temp.inStock - ltlt setprecision( 2 ) ltlt setw( 10 )
ltlt temp.unitPrice ltlt '\n' -
-
-
- void updateRecord( fstream fRef )
-
- Data temp
- int part
- cout ltlt "Enter the part number for update "
- cin gtgt part
- fRef.seekg( part sizeof( Data ) )
- fRef.read( reinterpret_castlt char gt( temp
), sizeof( Data ) ) - if ( temp.partNumber ! -1 )
- cout ltlt setw( 7 ) ltlt "Record" ltlt " " ltlt
setiosflags( iosleft ) - ltlt setw( 30 ) ltlt "Tool name" ltlt
resetiosflags( iosleft ) - ltlt setw( 13 ) ltlt "Quantity" ltlt setw(
10 ) ltlt "Cost\n" - cout.setf( iosfixed iosshowpoint )
- cout ltlt setw( 7 ) ltlt temp.partNumber ltlt "
" - ltlt setiosflags( iosleft ) ltlt setw(
30 ) ltlt temp.toolName - ltlt resetiosflags( iosleft ) ltlt setw(
13 ) ltlt temp.inStock
8Source Code Page 4
- fRef.seekp( ( temp.partNumber ) sizeof( Data )
) - fRef.write( reinterpret_castlt char gt (
temp ), sizeof( Data ) ) -
- else
- cerr ltlt "Cannot update. The record is
empty.\n" -
- void insertRecord( fstream fRef )
-
- Data temp
- int part
- cout ltlt "Enter the partnumber for insertion
" - cin gtgt part
- fRef.seekg( ( part ) sizeof( Data ) )
- fRef.read( reinterpret_castlt char gt ( temp
), sizeof( Data ) ) - if ( temp.partNumber -1 )
- temp.partNumber part
- fRef.seekp( ( temp.partNumber ) sizeof( Data )
) - fRef.write( reinterpret_castlt char gt(
temp ), sizeof( Data ) ) -
- else
- cerr ltlt "Cannot insert. The record contains
information.\n" -
- void deleteRecord( fstream fRef )
-
- Data blankItem -1, "", 0, 0.0 , temp
- int part
- cout ltlt "Enter the partnumber for deletion "
- cin gtgt part
- fRef.seekg( part sizeof( Data ) )
- fRef.read( reinterpret_castlt char gt( temp
), sizeof( Data ) ) - if ( temp.partNumber ! -1 )
9Output