home account info subscribe login search My ITKnowledge FAQ/help site map contact us


 
Brief Full
 Advanced
      Search
 Search Tips
To access the contents, click the chapter and section titles.

Learn Pascal in a Three Days (2nd Ed.)
(Publisher: Wordware Publishing, Inc.)
Author(s):
ISBN: 1556225679
Publication Date: 07/01/97

Bookmark It

Search this book:
 
Previous Table of Contents Next


10-3 Deleting Records from the File

The algorithm to delete an employee record from the payroll file is as follows:

1.  Enter the social security number of the employee to be removed.
2.  Open the payroll file for reading, and a temporary file for writing.
3.  Read the payroll file up to the end-of-file. For each record, check the SSN field against the social security number.
4.  Copy each record, except the one that matches, to the temporary file.
5.  Copy the temporary file into the original payroll file.
6.  Delete the temporary file.

To do that you need to add the following procedure to the program. (The source code of this procedure is on the companion disk under the name “del-proc.pas”.)

{ ------------------------- Procedure DelRec ------------------------- }
PROCEDURE DelRec(VAR NewFile, PayrollFile:TEXT;
                     Employee:EmployeeRecord);
VAR
  SSNumber:STRING[11];
BEGIN
  ASSIGN(PayrollFile, FileName);
  RESET(PayrollFile);
  ASSIGN(NewFile, TempFile);
  REWRITE(NewFile);
  WRITE('Please enter the SSN of the employee to be deleted: ');
  READLN(SSNumber);
  WHILE NOT EOF(PayrollFile) DO
    BEGIN
     WITH Employee DO
      BEGIN
        READLN(PayrollFile, ID);
        READLN(PayrollFile, Name);
        READLN(PayrollFile, Position);
        READLN(PayrollFile, SSN);
        READLN(PayrollFile, Category);
        CASE Category OF
         '1': READLN(PayrollFile, MonthlySalary);
         '2': READLN(PayrollFile, HourlyRate);
         '3': BEGIN
               READLN(PayrollFile, Commission);
               READLN(PayrollFile, BasicSalary);
               READLN(PayrollFile, Area)
              END
        END; { End of CASE structure }
        IF SSNumber <> SSN THEN
          BEGIN
           WRITELN(NewFile, ID);
           WRITELN(NewFile, Name);
           WRITELN(NewFile, Position);
           WRITELN(NewFile, SSN);
           WRITELN(NewFile, Category);

            CASE Category OF
             '1': WRITELN(NewFile, MonthlySalary:0:2);
             '2': WRITELN(NewFile, HourlyRate:0:2);
             '3': BEGIN
                   WRITELN(NewFile, Commission:0:2);
                   WRITELN(NewFile, BasicSalary:0:2);
                   WRITELN(NewFile, Area)
                  END
            END;          { End of CASE structure }
         END
      END        { End of WITH block }
    END;
  CLOSE(NewFile);
  CLOSE(PayrollFile);
{ Copy NewFile back to Payroll File }
  ASSIGN(PayrollFile, FileName);
  REWRITE(PayrollFile);
  ASSIGN(NewFile, TempFile);
  RESET(NewFile);
  WHILE NOT EOF(NewFile) DO
   BEGIN
    READLN(NewFile, OneLine);
    WRITELN(PayrollFile, OneLine)
   END;
  CLOSE(NewFile);
  ERASE(NewFile);    { Erase the temporary file }
  CLOSE(PayrollFile);
  WRITELN('The employee ', SSNumber, ' is removed from file.')
  END;

In order to have the “Delete Record” option as one of the menu items, you need to modify the “Menu” procedure. It may look similar to the procedure below (this procedure is on the companion disk under the name “mnu-proc.pas”).

{ ------------------------- Procedure Menu --------------------------- }
PROCEDURE Menu;
VAR
  Option:INTEGER;
BEGIN
  WRITELN(Header);
  WRITELN;
  WRITELN('1. Display an employee record.');
  WRITELN('2. Add a new employee.');
  WRITELN('3. Delete an employee.');
  WRITELN('4. Exit.');
  WRITELN(Separator);
  WRITE('Make a choice and press a number: ');
  READLN(Option);
  CASE Option OF
    1: ReadRec(PayrollFile, EmployeeRec);
    2: AddRec(NewFile, PayrollFile, EmployeeRec);
    3: DelRec(NewFile, PayrollFile, EmployeeRec);
    4: Exit
  END;
  Menu
END;

Sample run:

The following is a sample run to delete the record of the employee whose SSN is 347-12-3456. The user input is italicized and bolded for clarity.

------------- Main Menu --------------
1. Display an employee record.
2. Add a new employee.
3. Delete an employee.
4. Exit.
--------------------------------------
Make a choice and press a number: 3
Please enter the SSN of the employee to be deleted: 347-12-3456
The employee 347-12-3456 is removed from file.

------------- Main Menu --------------
1. Display an employee record.
2. Add a new employee.
3. Delete an employee.
4. Exit.
--------------------------------------
Make a choice and press a number: 4

Drill 10-2

In the previous “DelRec” procedure, the program will send the message “The employee … is removed from file,” whether or not the “SSN” is in the file.

Add the necessary code to make the program send the proper message in each case.


Previous Table of Contents Next


Products |  Contact Us |  About Us |  Privacy  |  Ad Info  |  Home

Use of this site is subject to certain Terms & Conditions, Copyright © 1996-2000 EarthWeb Inc.
All rights reserved. Reproduction whole or in part in any form or medium without express written permission of EarthWeb is prohibited. Read EarthWeb's privacy statement.