I was composing a post earlier this afternoon about
grandtotals but decided that we need to cover some other ground before I can adequately explain. Below is some code I have extracted from your posts. It is just as you posted except I have tried to restore some indentation (which I hope you are using) and omitted some blank lines.
I want to discuss your procedure
add.
(*184*)
(*185*) Procedure Add;
(*186*) {*************************************************************************
(*187*) * This Procedure is used to ask the user which database he would like to *
(*188*) * use to add records to the respective groups. *
(*189*) *************************************************************************}
(*190*) var
(*191*) Ans : char;
(*192*) choice:integer;
(*193*) begin
(*194*) clrscr;
(*195*)
(*196*) Textcolor(Black);
(*197*) Textbackground(White);
(*198*) GotoXY(8,2);
(*199*) Writeln ('ÉÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍ»');
(*200*) Gotoxy(8,3);
(*201*) Writeln ('º º');
(*202*) Gotoxy(8,4);
(*203*) Writeln ('º Welcome to the Add option of this database. º');
(*204*) Gotoxy(8,5);
(*205*) Writeln ('º Here you can add members in different groups . º');
(*206*) Gotoxy(8,6);
(*207*) Writeln ('º Enjoy! º');
(*208*) Gotoxy(8,7);
(*209*) Writeln ('º º');
(*210*) Gotoxy(8,8);
(*211*) Writeln ('ÈÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍÍͼ');
(*212*)
(*213*) WRITELN;
(*214*) Writeln('Here you will choose which database you want to add someone to.');
(*215*) Writeln;
(*216*) Writeln('1-Add person to DDC database.');
(*217*) Writeln('2-Add person to DPI database.');
(*218*) Writeln('3-Add person to DSGO database.');
(*219*) Writeln('Press any number more than "3" to go to main menu');
(*220*) Writeln;
(*221*) Writeln('Press the respective number then press Enter to continue.');
(*222*) Readln(choice);
(*223*)
(*224*) case choice of
(*225*) 1:Add_MemberDDC;
(*226*) 2:Add_MemberDPI;
(*227*) 3:Add_MemberDSGO;
(*228*) END;
(*229*)
(*230*) Readln;
(*231*) Mainmenu;
(*232*)
Note lines 225, 226, 227. Depending on the value of
choice the program calls one of three procedures:
Add_MemberDDC, Add_MemberDPI or
Add_MemberDSGO. Examination of these three procedures shows that they are nearly identical, the differences being which file the procedure accesses.
Add_MemberDDC accesses file
DDCDatabasefile,
Add_MemberDPI accesses
DPIDatabasefile and
Add_MemberDSGO accesses
DSGODatabasefile.
This makes for a lot of duplicated code. It would be great to have just one procedure that could handle all three files. The problem with that is that all three files have different structures. One solution to that is to simply write three different procedures (your solution). This is a legitimate choice. Another is to load the procedure with a lot of if..then..else statements (or case statements). Very ugly and I don’t recommend it.
My solution is to find a structure that satisfies the requirements of all three databases, such as ...
Type
MemberDatabase = record
Member_ID : string[10] ;
Member_Name : string[30] ;
Member_TicketPayment : integer ;
Advert : string
End ;
The obvious objection to such an approach is that only the DDC database uses the field
Advert. Its inclusion in the DPI and DSGO databases wastes hard disk space. At one time, when hard disk space was expensive, that was a valid objection but today disk space is cheap enough (measured in gigibytes) that its expenditure is a small price to pay for simpler, more elegant code.
If we adopt this approach then we need to declare the following.
Type
{
this declaration is necessary because
“file of MemberDatabase” cannot be passed
to a procedure or function.
}
Databasefiletype = file of MemberDatabase ;
var
DDCDatabasefile,
DPIDatabasefile,
DSGODatabasefile : Databasefiletype ;
and to make the assignments
begin { main program }
assign(DDCDatabasefile, ‘memberdb.ddc’) ;
assign(DPIDatabasefile, ‘memberdb.dpi’) ;
assign(DSGODatabasefile, ‘memberdb.dsg’) ;
viewmenu ;
mainmenu
end. { main program }
And now lines 224 .. 228 become
(*224*) case choice of
(*225*) 1 : Add_Member (DDCDatabasefile, 'DDC') ;
(*226*) 2 : Add_Member (DPIDatabasefile, 'DPI') ;
(*227*) 3 : Add_Member (DSGODatabasefile, 'DSGO')
(*228*) END;
and the single procedure
Add_Member becomes
Procedure Add_Member (var databasefile : Databasefiletype ; db : string) ;
{*****************************************
* This Procedure is used to add members *
* into any database *
*****************************************}
Var
Databaserec : Memberdatabase ;
BEGIN
{$I-}
clrscr;
Reset(Databasefile);
If IOResult <>0 then
Rewrite(Databasefile);
Textcolor(Black);
Textbackground(White);
GotoXY(23,8);
Writeln('------------------------------------------------');
GotoXY(23,9);
Writeln('------------------------------------------------');
GotoXY(32,10);
Writeln(' Welcome to the ', db, ' database. ');
GOTOXY(23,11);
Writeln(' ');
GOTOXY(23,11);
Writeln('-----------------------------------------------');
Gotoxy(23,12);
Writeln('----------------------------------------------- ');
With databaserec do begin
Writeln;
write ('Enter the Member ID.');
readln(Member_ID);
Writeln;
write ('Enter the Member full name.');
Readln(Member_Name);
Writeln;
repeat
write ('Enter the Member Ticket Price');
readln(Member_TicketPayment);
writeln
until ioresult = 0
end ;
seek(Databasefile, filesize(Databasefile));
Write(Databasefile, DatabaseRec);
Write('Now there are ', fileSize(databasefile), ' records in the ', db, ' Database ');
ReadLn;
Close(Databasefile)
{$I+}
end ;
If I may be permitted a small digression and allowed to be Sherlock Holmes, I observe that lines 33, 85 and 136 all have the same typographical error. From this I deduce that you wrote one of the procedures, then copied and pasted it twice and made the necessary modifications.