{******************************************************************** * Task Task 17 Updating a Sequential File * * Date written : 25 April 2006 * * Author : CW Chan * ********************************************************************} program t17; const deleted = -1; cls_size = 45; var option : char; class : string[2]; nstudent : integer; classfile : text; admno : array [1..cls_size] of integer; mark : array [1..cls_size] of integer; name : array [1..cls_size] of string[20]; procedure readdata; var i : integer; begin {readdata} assign(classfile, class + 'mark.dat'); reset(classfile); i := 0; while not eof(classfile) do begin i := i + 1; readln(classfile, admno[i]); readln(classfile, name[i]); readln(classfile, mark[i]); end; nstudent := i; close(classfile); end; {readdata} procedure dispmenu (var choice : char); {dispmenu} begin writeln; writeln(' (1) Add records '); writeln(' (2) Delete records '); writeln(' (3) Change records '); writeln(' (0) Quit '); writeln; repeat write(' Choice : '); readln(option); if (choice < '0') or (choice > '3') then begin writeln(' Invalid choice !'); writeln; end; until (choice >= '0') and (choice <= '3'); writeln; end; {dispmenu} procedure add; var id, i : integer; ans : char; found : boolean; begin {add} writeln('Add Records'); writeln('==========='); repeat writeln; write('Enter admission number : '); readln(id); i := 1; found := false; while (not found) and (i <= nstudent) do begin if id = admno[i] then begin found := true; writeln('Student already in the list found !'); end else i := i + 1; end; if (not found) then begin nstudent := nstudent + 1; admno[nstudent] := id; write('Enter student''s name : '); readln(name[nstudent]); write('Enter student''s mark : '); readln(mark[nstudent]); writeln('Record added !'); writeln; end; write('Any more (y/n) : '); readln(ans); until (ans = 'N') or (ans ='n') ; end; {add} procedure delete; var id, i : integer; ans : char; found : boolean; begin {delete} writeln('Delete Records'); writeln('=============='); repeat writeln; write('Admission no. of student to be deleted : '); readln(id); i := 1; found := false; while (not found) and (i <= nstudent) do begin if id = admno[i] then begin found := true; admno[i] := deleted; writeln('Record deleted !'); writeln; end else i := i + 1; end; if (not found) then writeln('Student not found !'); write('Any more (y/n) : '); readln(ans); until (ans = 'N') or (ans ='n') ; end; {delete} procedure change; var id, i : integer; ans : char; found : boolean; begin {change} writeln('Change Records'); writeln('=============='); repeat writeln; write('Admission no. of student to be updated : '); readln(id); i := 1; found := false; while (not found) and (i <= nstudent) do begin if id = admno[i] then begin found := true; writeln(name[i]); write('Test Mark change from ',mark[i],' to : '); readln(mark[i]); writeln('Record updated !'); writeln; end else i := i + 1; end; if (not found) then writeln('Student not found !'); write('Any more (y/n) : '); readln(ans); until (ans = 'N') or (ans ='n') ; end; {change} procedure update; var i : integer; begin {update} assign(classfile, class + 'mark.dat'); rewrite(classfile); for i := 1 to nstudent do begin if admno[i] <> deleted then begin writeln(classfile, admno[i]); writeln(classfile, name[i]); writeln(classfile, mark[i]); end; end; close(classfile); end; {update} procedure quit; begin {quit} update; writeln('Bye-bye !'); end; {quit} begin {main} write('Class : '); readln(class); readdata; repeat dispmenu(option); case option of '1' : add; '2' : delete; '3' : change; '0' : quit; end; until option = '0'; end. {main}