Help(Project crisis) - Programmers Heaven

Howdy, Stranger!

It looks like you're new here. If you want to get involved, click one of these buttons!

Categories

Welcome to the new platform of Programmer's Heaven! We apologize for the inconvenience caused, if you visited us from a broken link of the previous version. The main reason to move to a new platform is to provide more effective and collaborative experience to you all. Please feel free to experience the new platform and use its exciting features. Contact us for any issue that you need to get clarified. We are more than happy to help you.

Help(Project crisis)

pampazpampaz Posts: 10Member
hey guys a new programmer here..well i'm still in high school and we're using turbo pascal and i have this Major project doing and i am currently making a database in a program .. using "assigns" "rewrite" and the famous I/O Results. These parts are going fine but when i viewed my program object code( the people and their information) i can only see the last two people and their information ..my teacher said to get codes to scroll up and down O_O ( like you're supposed to give them to me). any ways this project is real important and i was wandering if you guys know some codes to scroll up and down. please send me the link(s) . Plus i'm using turbo pascal 7.0 16 bit. or i can upload my program and you can just input the codes for viewing the information.

thanks...
oh and can any one tell me how to make a box using alt

the scrolling codes have to go in view_member(dda,dpi and dsgo)


Program schoolSBA;

uses crt, dos;

Type
MemberDatabaseDDC = record {declaration of record}

Member_ID : string[10];
Member_Name:String[30];
Member_Address :String[50];
Member_Phone : String[14];
Member_Age:string[3];
Member_TicketPayment:Real;

Advert:string;
end;

MemberDatabaseDPI=record
Member_ID : string[10];
Member_Name:String[30];
Member_Address :String[50];
Member_Phone : String[14];
Member_Age:string[3];
Member_TicketPayment:Real;
end;

MemberDatabaseDSGO = record
Member_ID : string[10];
Member_Name:String[30];
Member_Address :String[50];
Member_Phone : String[14];
Member_Age:string[3];
Member_TicketPayment:Real;
end;

var
DDCDatabasefile : file of MemberDatabaseDDC;
DatabaseRecDDC: MemberDatabaseDDC; {variable for DDC record}

DPIDatabasefile: file of MemberDatabaseDPI;
DatabaseRecDPI: MemberDatabaseDPI;{variable for DPI record}

DSGODatabasefile : file of MemberDatabaseDSGO;
DatabaseRecDSGO :MemberDatabaseDSGO; {variable for DSGO record}

Choice, count,i: integer;
Opened : Boolean;
Databasename : String;


Procedure Mainmenu; Forward;

{*************************************************
*This Procedure is used to display the Main Menu *
*of the program. *
**************************************************}

Procedure Viewmenu;
{Procedure to display intro}
Begin
clrscr;


TextColor(red+black);
Textbackground(black);
Clrscr;
GotoXY (1,4);
WriteLn( '
«1345

Comments

  • ActorActor Posts: 438Member
    Your code does not seem to have any procedure or function named "view_member."

    By the way "exit" is not a good name for an identifier because Pascal already has a procedure named "exit," which seems to do exactly what you want your procedure to do, cause an immediate exit from the current procedure.

  • pampazpampaz Posts: 10Member
    Yes there is just look for it their names' are view_memberddc..if u can give me a code to draw a scroll bar in the object code to view them all that would be great. Thanks
  • pampazpampaz Posts: 10Member
    Yes there is just look for it their names' are view_memberddc..if u can give me a code to draw a scroll bar in the object code to view them all that would be great. Thanks
  • ActorActor Posts: 438Member
    OK. I found it. The procedure is [b] View_RecordsDDC[/b].

    A scroll bar is not possible with Turbo Pascal.

    This should be close to what you need. The program begins by displaying [b]SIZE[/b] records ending with the "current record." [b]SIZE[/b] is a [b]const[/b] set to 2 (line 63) but can be easily changed and the program recompiled.

    The current record is initially = 1. The operator changes this value by hitting certain keys on the keyboard. It is not necessary to hit the ENTER key. Hitting the "up arrow" key increments the current record by one. Hitting the "down arrow" decrements it by one. PageUp increments it by 10 (the value of PGVAL
  • pampazpampaz Posts: 10Member
    OMG thank you very much ......it worked ..your a genius . I just executed the program and it is working good i just finished putting the same codes for the other views' i have. i just have to change the function name.

    so you were saying that i have recursive calls ..can u enlighten me on them so i can make my program alot better.

    O and if you still have a copy of my program i sent you i was wondering if you could get some codes for some animation in it...not too much but just enough to get a great mark.

    And the last thing is do you know codes to edit the info i added. cuz someone could put wrong information and have to change it . this would be deeply appreciated.

    And again thanks a Trillion........
  • ActorActor Posts: 438Member
    : so you were saying that i have recursive calls ..can u enlighten me
    : on them so i can make my program a lot better.

    Taking your questions one at a time, I'll respond to the others in later posts. Right now I'll try to handle recursion.

    Recursion is the term used when a procedure or function calls itself. The classic example is the factorial function. In mathematics the factorial of an integer [b]n[/b] is the cumulative product of all integers from 1 to n. Mathematicians use an exclamation point, [b]![/b] for the factorial of an integer. Thus:

    5! = 5 * 4 * 3 * 2 * 1 = 120

    Note that n! = n * (n - 1)! e.g. 5! = 5 * n!

    This program computes the factorial of an integer.
    [code]
    program nfact ;
    {
    compute the factorial of a number
    }
    function fact(n : longint) : longint ;
    {
    if n > 1 compute n(n - 1)!
    }
    begin
    if n = 1 then
    fact := 1
    else
    fact := n * fact(n - 1)
    end ;
    var
    f,n : longint ;
    begin
    write('Enter a number ') ;
    readln(n) ;
    f := fact(n) ;
    writeln(n:0, '! = ', f:0)
    end.
    [/code]
    The function [b]fact[/b] works by repeatedly multiplying [b]n[/b] by the the factorial of [b]n - 1[/b], unless [b]n[/b] = 1. 1! is, by definition, 1. If [b]n[/b] = 5 then [b]fact[/b] calls itself four times, a [i]depth[/i] of 4.

    [b]n[/b] is passed to [b]fact[/b] by value, not by reference, so each time [b]fact[/b] is called it has to make room in memory for [b]n[/b], since it cannot use the same space as the previous instance. Since we are invoking [b]fact[/b] 5 times (for [b]n[/b] = 5) and a longint takes up 4 bytes, the program takes 20 bytes of memory for [b]n[/b] = 5. If [b]n[/b] = 100 it would take up 400 bytes.

    Once [b]n[/b] = 1 the program begins to retreat our of the recursive hole. Each time it goes up a level it releases memory until it gets out of the hole.

    Every procedure or function that uses recursion should have some condition where is does not call itself, allowing it to retreat from the hole.

    There is a bug in the program [b]nfact[/b]. I left it there on purpose. If [b]n[/b] is zero or negative [b]n[/b] will never be equal to one. [b]fact[/b] will be called repeatedly until memory is used up and the program crashes.

    Recursion also occurs when a procedure calls a procedure that called it, e.g., [b]mainmenu[/b] calls [b]add[/b] which in turn calls [b]mainmenu[/b].

    In theory, anything that can be done with recursion can be done with [italic]iteration[/italic], i.e., [italic]looping[/italic]. For instance:
    [code]
    program nfact ;
    {
    compute the factorial of a number -- looping version
    }
    var
    i,f,n : longint ;
    begin
    write('Enter a number ') ;
    readln(n) ;
    f := 0 ;
    for i := n downto 1 do
    f := f * i ;
    writeln(n:0, '! = ', f:0)
    end.
    [/code]
    You're probably thinking that the looping version is simpler than the recursive version, and you're right. Some problems are simpler using loops, others are simpler using recursion. The [italic]quicksort[/italic] algorithm is simpler using recursion, complex using looping.

    Now let's consider one of your procedures.
    [code]
    Procedure Add;

    var
    Ans : char;
    choice:integer;
    begin
    clrscr;

    Textcolor(Green);
    Textbackground(Black);
    {
    yatta ...
    yatta ...
    yatta ...
    }
    Writeln('Press the respective number then press " Enter" to continue');
    Readln(choice);

    case choice of
    1 : Add_MemberDDC;
    2 : Add_MemberDPI;
    3 : Add_MemberDSGO;
    4 : Mainmenu;
    else
    Mainmenu;
    end;
    END;
    [/code]
    After the user makes his "choice" the program either invokes one of the Add_MemberXXX procedures or else invokes another instance of [b]Mainmenu[/b]. Is this what you want? I think not. I think what you want is to return to the previous instance of [b]Mainmenu[/b], not create another one.

    There are two ways to return to the previous instance. One is to simply reach the end of the procedure, the "END;" in this case. The other way is to invoke the Pascal procedure [b]exit[/b]. Thus:
    [code]
    Procedure Add;

    var
    Ans : char;
    choice:integer;
    begin
    clrscr;

    Textcolor(Green);
    Textbackground(Black);
    {
    yatta ...
    yatta ...
    yatta ...
    }
    Writeln('Press the respective number then press " Enter" to continue');
    Readln(choice);

    case choice of
    1 : Add_MemberDDC;
    2 : Add_MemberDPI;
    3 : Add_MemberDSGO;
    end;
    END;
    [/code]
    or
    [code]
    Procedure Add;

    var
    Ans : char;
    choice:integer;
    begin
    clrscr;

    Textcolor(Green);
    Textbackground(Black);
    {
    yatta ...
    yatta ...
    yatta ...
    }
    Writeln('Press the respective number then press " Enter" to continue');
    Readln(choice);

    case choice of
    1 : Add_MemberDDC;
    2 : Add_MemberDPI;
    3 : Add_MemberDSGO;
    else
    exit ;
    end;
    END;
    [/code]

  • ActorActor Posts: 438Member
    :
    : And the last thing is do you know codes to edit the info i added.
    : cuz someone could put wrong information and have to change it . this
    : would be deeply appreciated.
    :
    Here's a program I wrote that relates to your problem. Unfortunately it's so big that I won't be able to properly discuss it without running out of the space allotted for a single post so I'll discuss it in a separate post.
    [code]
    { 1} program file_editor ; uses crt ;
    { 2} {
    { 3} simple file editor -- records student id, name, sex, age, class and grade point average
    { 4} }
    { 5} CONST
    { 6} NUL = chr(0) ;
    { 7} BLANK = ' ' ;
    { 8} ID_LEN = 9 ;
    { 9} NAME_LEN = 20 ;
    { 10}
    { 11} FILENAME = 'student.dat' ;
    { 12} SCRATCH = 'scratch' ;
    { 13}
    { 14} type
    { 15} studenttype = record
    { 16} id : string[ID_LEN] ;
    { 17} lastname : string[NAME_LEN] ;
    { 18} firstname : string[NAME_LEN] ;
    { 19} mid_init : char ;
    { 20} sex : char ;
    { 21} age : byte ;
    { 22} class : 0 .. 12 ;
    { 23} gpa : real
    { 24} end ;
    { 25}
    { 26} file_of_studenttype = file of studenttype ;
    { 27}
    { 28} procedure brag ;
    { 29} {
    { 30} introduction
    { 31} }
    { 32} CONST
    { 33} BRAGTEXT = 'A computer program by J. Delano Actor!' ;
    { 34} MIDLINE = 12 ;
    { 35}
    { 36} begin { brag }
    { 37} clrscr ;
    { 38} gotoxy((80 - length(BRAGTEXT)) div 2, MIDLINE) ;
    { 39} write(BRAGTEXT) ; { centered }
    { 40} readkey
    { 41} end ; { brag }
    { 42}
    { 43} procedure app ;
    { 44} {
    { 45} appends a new record to the file
    { 46} }
    { 47} CONST
    { 48} ID_ROW = 3 ;
    { 49} LNAME_ROW = ID_ROW + 2 ;
    { 50} FNAME_ROW = ID_ROW + 4 ;
    { 51} MI_ROW = ID_ROW + 6 ;
    { 52} SEX_ROW = ID_ROW + 8 ;
    { 53} AGE_ROW = ID_ROW + 10 ;
    { 54} CLASS_ROW = ID_ROW + 12 ;
    { 55} GPA_ROW = ID_ROW + 14 ;
    { 56} SPACES = ' ' ;
    { 57}
    { 58} procedure app_menu ;
    { 59} {
    { 60} writes append menu to screen
    { 61} }
    { 62} begin { app_menu }
    { 63} clrscr ;
    { 64} writeln ;
    { 65} writeln ;
    { 66} writeln('1. IDENTIFICATION :':30) ;
    { 67} writeln ;
    { 68} writeln('2. LAST NAME :':30) ;
    { 69} writeln ;
    { 70} writeln('3. FIRST NAME :':30) ;
    { 71} writeln ;
    { 72} writeln('4. MIDDLE INITIAL :':30) ;
    { 73} writeln ;
    { 74} writeln('5. GENDER :':30) ;
    { 75} writeln ;
    { 76} writeln('6. AGE :':30) ;
    { 77} writeln ;
    { 78} writeln('7. CLASS :':30) ;
    { 79} writeln ;
    { 80} writeln('8. GRADE POINT AVERAGE :':30) ;
    { 81} writeln ;
    { 82} writeln ;
    { 83} writeln('A. APPEND TO FILE AND QUIT APPEND MODE':44) ;
    { 84} writeln ;
    { 85} writeln('Q. QUIT APPEND MODE WITHOUT SAVING DATA':45)
    { 86} end ; { app_menu }
    { 87}
    { 88} procedure clear (var student : studenttype) ;
    { 89} {
    { 90} clear student record
    { 91} }
    { 92} begin { clear }
    { 93} with student do begin
    { 94} id := '' ;
    { 95} lastname := '' ;
    { 96} firstname := '' ;
    { 97} mid_init := NUL ;
    { 98} sex := NUL ;
    { 99} age := 0 ;
    {100} class := 0 ;
    {101} gpa := 0.0
    {102} end
    {103} end ; { clear }
    {104}
    {105} procedure show (const student : studenttype) ;
    {106} {
    {107} show record on screen
    {108} }
    {109} begin { show }
    {110} with student do begin
    {111} gotoxy(32, ID_ROW) ; write(id) ;
    {112} gotoxy(32, LNAME_ROW) ; write(lastname) ;
    {113} gotoxy(32, FNAME_ROW) ; write(firstname) ;
    {114} gotoxy(32, MI_ROW) ; write(mid_init) ;
    {115} gotoxy(32, SEX_ROW) ; write(sex) ;
    {116} gotoxy(32, AGE_ROW) ; write(age) ;
    {117} gotoxy(32, CLASS_ROW) ; write(class) ;
    {118} gotoxy(32, GPA_ROW) ; write(gpa:0:3) ; { avoid E formatting }
    {119}
    {120} gotoxy(79, 24) { move cursor out of the way }
    {121} end
    {122} end ; { show }
    {123}
    {124} function getstr (row : byte) : string ;
    {125} {
    {126} get a string from a defined place on the screen
    {127} }
    {128} var
    {129} str : string ;
    {130} begin { getstr }
    {131} gotoxy(32,row) ;
    {132} write(SPACES) ; { erase previous contents from screen }
    {133} gotoxy(32,row) ;
    {134} readln(str) ;
    {135} getstr := str
    {136} end ; { getstr }
    {137}
    {138} function getch (row : byte) : char ;
    {139} {
    {140} get a character from a defined place on the screen
    {141} }
    {142} var
    {143} str : string ;
    {144} begin { getch }
    {145} str := getstr(row) ;
    {146} if length(str) > 0 then
    {147} getch := str[1]
    {148} else
    {149} getch := NUL
    {150} end ; { getch }
    {151}
    {152} function getint (row : byte) : integer ;
    {153} {
    {154} get an integer from a defined place on the screen
    {155} }
    {156} var
    {157} i : integer ;
    {158} begin { getint }
    {159} {$I-}
    {160} gotoxy(32,row) ;
    {161} write(SPACES) ; { erase previous contents from screen }
    {162} gotoxy(32,row) ;
    {163} readln(i) ;
    {164} if ioresult = 0 then
    {165} getint := i
    {166} else
    {167} getint := 0
    {168} {$I+}
    {169} end ; { getint }
    {170}
    {171} function getreal (row : byte) : real ;
    {172} {
    {173} get a real number from a defined place on the screen
    {174} }
    {175} var
    {176} x : real ;
    {177} begin { getreal }
    {178} {$I-}
    {179} gotoxy(32,row) ;
    {180} write(SPACES) ; { erase previous contents from screen }
    {181} gotoxy(32,row) ;
    {182} readln(x) ;
    {183} if ioresult = 0 then
    {184} getreal := x
    {185} else
    {186} getreal := 0.0
    {187} {$I+}
    {188} end ; { getreal }
    {189}
    {190} procedure save (student : studenttype) ;
    {191} {
    {192} append a record to the file
    {193} }
    {194} procedure open (var f : file_of_studenttype) ;
    {195} {
    {196} open a typed file for appending
    {197} }
    {198} begin { open }
    {199} {$I-}
    {200} reset(f) ;
    {201} if ioresult > 0 then begin
    {202} rewrite(f) ;
    {203} if ioresult > 0 then begin
    {204} write('Cannot open file ', FILENAME) ;
    {205} halt { give up and quit program }
    {206} end
    {207} end
    {208} {$I+}
    {209} end ; { open }
    {210}
    {211} var
    {212} f : file_of_studenttype ;
    {213} n : longint ;
    {214} begin { save }
    {215} assign(f, FILENAME) ;
    {216} open(f) ;
    {217} n := filesize(f) ;
    {218} seek(f,n) ;
    {219} write(f, student) ;
    {220} close(f)
    {221} end ; { save }
    {222}
    {223} var
    {224} student : studenttype ;
    {225} key : char ;
    {226} begin { app }
    {227} clear(student) ;
    {228} repeat
    {229} app_menu ;
    {230} show(student) ;
    {231}
    {232} repeat
    {233} key := upcase(readkey)
    {234} until key in ['1' .. '8', 'A', 'Q'] ;
    {235}
    {236} with student do begin
    {237} case key of
    {238} '1' : id := getstr(ID_ROW) ;
    {239} '2' : lastname := getstr(LNAME_ROW) ;
    {240} '3' : firstname := getstr(FNAME_ROW) ;
    {241} '4' : mid_init := getch(MI_ROW) ;
    {242} '5' : sex := getch(SEX_ROW) ;
    {243} '6' : age := getint(AGE_ROW) ;
    {244} '7' : class := getint(CLASS_ROW) ;
    {245} '8' : gpa := getreal(GPA_ROW) ;
    {246} 'A' : begin
    {247} save(student) ;
    {248} key := 'Q'
    {249} end
    {250} end { case }
    {251} end
    {252} until key = 'Q'
    {253} end ; { app }
    {254}
    {255} procedure view ;
    {256} {
    {257} view the database
    {258} }
    {259} procedure heading ;
    {260} {
    {261} header for the 'view' screen
    {262} }
    {263} begin { heading }
    {264} write('Rec #') ;
    {265} write('ID #':ID_LEN + 1) ;
    {266} write('LAST NAME':NAME_LEN + 1) ;
    {267} write('FIRST NAME':NAME_LEN + 1) ;
    {268} write('MI':3) ;
    {269} write('SEX':4) ;
    {270} write('AGE':4) ;
    {271} write('CLS':4) ;
    {272} write('GPA':7) ;
    {273} writeln ;
    {274}
    {275} write('-----') ; { record number }
    {276} write(' ---------') ; { id }
    {277} write(' --------------------') ; { last name }
    {278} write(' --------------------') ; { first name }
    {279} write(' --') ; { middle initial }
    {280} write(' -') ; { sex }
    {281} write(' --') ; { age }
    {282} write(' --') ; { class }
    {283} write(' -----') ; { grade point average }
    {284} writeln
    {285} end ; { heading }
    {286}
    {287} procedure open (var f : file_of_studenttype) ;
    {288} {
    {289} open file for reading -- one way or another
    {290} }
    {291} begin { open }
    {292} {$I-}
    {293} reset(f) ;
    {294} if ioresult > 0 then begin
    {295} assign(f, 'NUL') ; { the bit bucket -- always empty }
    {296} reset(f)
    {297} end
    {298} {$I+}
    {299} end ; { open }
    {300} var
    {301} student : studenttype ;
    {302} f : file_of_studenttype ;
    {303} i,n : longint ;
    {304}
    {305} begin { view }
    {306} clrscr ;
    {307} heading ;
    {308} assign(f, FILENAME) ;
    {309} open(f) ;
    {310} n := filesize(f) - 1 ; { index of last record }
    {311} for i := 0 to n do begin
    {312} seek(f, i) ;
    {313} read(f, student) ;
    {314} with student do begin
    {315} write(i:5) ;
    {316} write(id:ID_LEN + 1) ;
    {317} write(lastname:NAME_LEN + 1) ;
    {318} write(firstname:NAME_LEN + 1) ;
    {319} write(mid_init:3) ;
    {320} write(sex:4) ;
    {321} write(age:4) ;
    {322} write(class:4) ;
    {323} write(gpa:7:3) ;
    {324} writeln
    {325} end
    {326} end ;
    {327} close(f) ;
    {328} writeln ;
    {329} writeln ;
    {330} writeln ;
    {331} writeln('Hit any key to continue') ;
    {332} readkey
    {333} end ; { view }
    {334}
    {335} procedure menu ;
    {336} {
    {337} shows the top level menu
    {338} }
    {339} begin { menu }
    {340} clrscr ;
    {341} writeln ;
    {342} writeln ;
    {343} writeln ;
    {344} writeln ;
    {345} writeln ;
    {346} writeln ;
    {347} writeln ;
    {348} writeln ;
    {349} writeln(BLANK:25, 'A. Append new record to file') ;
    {350} writeln ;
    {351} writeln(BLANK:25, 'V. View the file') ;
    {352} writeln ;
    {353} writeln(BLANK:25, 'Q. Quit program')
    {354} end ; { menu }
    {355}
    {356} var
    {357} key : char ;
    {358}
    {359} begin { file_editor }
    {360} brag ;
    {361} repeat
    {362} menu ;
    {363} repeat
    {364} key := upcase(readkey)
    {365} until key in ['A', 'V', 'Q'] ;
    {366} case key of
    {367} 'A' : app ;
    {368} 'V' : view ;
    {369} end { case }
    {370} until key = 'Q' ;
    {371} clrscr
    {372} end. { file_editor }
    [/code]
  • ActorActor Posts: 438Member
    The program I posted above is a simple file editor similar to the one you're writing. A file editor should be able to do at least three things.

    * Append (i.e., add) records to the file
    * Delete records from the file
    * Edit (i.e., change) records in the file

    The program I posted above only appends, which may not seem to helpful, but bear with me. Even though it only appends, it allows you to see your data and correct it before committing yourself to adding it to your file.

    Let's begin by examining the main body of the program, lines 359 .. 372. After displaying the [b]brag[/b] screen the program goes into a repeat .. until loop which terminates when [b]key = 'Q'[/b], i.e., the operator hits the 'Q' key at an appropriate time.

    This looping behavior makes the program "event driven" like most Windows applications. The program responds to events (keystrokes). The main routine only responds to 'A', 'V' and 'Q'. Anything else is ignored. Lines 363 .. 365.

    [b]brag[/b] and [b]menu[/b] are set apart in their own procedures rather than included in the main procedure. This is done mainly to keep the main routing uncluttered. At only 14 lines it is clean and easy to read and understand.

    Hitting 'V' invokes [b]view[/b], lines 255 .. 333, which displays the database. I kept [b]view[/b] simple because a record can be displayed on only one line and I don't foresee my test file getting big enough to require scrolling. However, there is one part of [b]view[/b] that I want to point out, the procedure [b]open[/b]. I wrote [b]open[/b] to keep the code for [b]view[/b] uncluttered. All the stuff about turning error detection off/on and testing [b]ioresult[/b] is shuffled off to a corner where we deal with it separately.

    Note that [b]open[/b] is nested inside [b]view[/b]. Other procedures cannot see it. I'll explain why that's important shortly.

    Meanwhile, if [b]reset[/b] fails, [b]open[/b] reassigns the file to 'NUL', the DOS/Windows bit bucket. 'NUL' is always empty. [b]eof[/b] is always TRUE for 'NUL'. [b]view[/b] will display an empty screen, hopefully gently reminding the operator that he has not entered any data yet, or that something is wrong.

    On to [b]app[/b], the procedure you are really interested in (I hope). I named it [b]app[/b] instead of [b]append[/b] to avoid a collision with the Pascal procedure [b]append[/b].

    Note that [b]app[/b] has it's own [b]open[/b] procedure nested within it. [b]view[/b] cannot see [b]app[/b]'s [b]open[/b] procedure, and vice versa, so each uses the correct procedure.

    [italic]By the way, some people may object to different procedures having the same name in the same program, even though nesting makes it clear which one is called. They argue that it makes the code harder to read, and would name one [b]open_read[/b] and the other [b]open_write[/b] or some such. I respectfully disagree.[/italic]

    [b]app[/b]'s [b]open[/b] procedure tries to [b]reset[/b] the file, if that fails it tries [b]rewrite[/b], then if that fails it gives up, writes and error message and terminates the program.

    [b]app[/b] is event driven, just like the main body. Hopefully you can see how it allows you to access each field of the record and enter data. If it's wrong you can redo it. When you finally think you have it right you hit 'A' and it gets appended to the file.

    Note that the procedures [b]getint[/b] and [b]getreal[/b] turn off error checking and use [b]ioresult[/b] to test for success. That's because [b]readln[/b] will return an "invalid numeric format" if the data you enter cannot be converted to a number, e.g., "3..14159" has too many decimal points and "one" is not recognized as an integer. In that event [b]getint[/b] returns 0 and [b]getreal[/b] returns 0.0 Hopefully the operator will recognize that something is wrong.

    One last thing: the program has the operator hit 'A' to append. Some programmers suggest that '^' or '&' should be used. The reason has to do with human engineering. Both '^' and '&' require that the shift key be held down, requiring two hands or an awkward one-hand operation. Either way it's less likely that the operator would do it accidentally and cause the data to be saved before he's finished.

    Hope this helps. Give me a day or so and I'll work on [b]del[/b] (don't want to collide with [b]delete[/b]) and [b]change[/b].

    Good luck.

  • ActorActor Posts: 438Member
    Here is the code for the [b]del[/b] procedure.
    [code]
    procedure del ;
    {
    deletes a record from the file
    }
    procedure open (var f : file_of_studenttype) ;
    {
    opens file for possible deletion of records
    }
    begin { open }
    {$I-}
    reset(f) ;
    if ioresult > 0 then begin
    assign(f, 'NUL') ; { the bit bucket }
    reset(f)
    end
    {$I+}
    end ; { open }

    procedure delete_rec (var f : file_of_studenttype ; n : longint) ;
    {
    delete designated record
    }
    procedure open (var g : file_of_studenttype ; var success : boolean) ;
    {
    open a scratch file
    }
    begin { open }
    {$I-}
    rewrite(g) ;
    success := (ioresult = 0)
    {$I+}
    end ; { open }
    var
    student : studenttype ;
    g : file_of_studenttype ;
    i : longint ;
    success : boolean ;
    begin { delete_rec }
    assign(g, SCRATCH) ;
    open(g, success) ;
    if success then begin
    for i := 0 to filesize(f) - 1 do begin
    seek(f,i) ;
    read(f,student) ;
    if i <> n then
    write(g, student)
    end
    end
    else begin
    writeln(BLANK:15, 'unable to create scratch file ') ;
    readln ;
    end ;
    {$I-}
    close(g) ;

    erase(f) ;
    rename(g, FILENAME)
    {$I+}
    end ; { delete_rec }

    var
    n : longint ;
    f : file_of_studenttype ;
    student : studenttype ;
    ch : char ;
    begin { del }
    clrscr ;
    gotoxy(1,12) ;

    write(BLANK:15, 'Enter the number of the record you wish to delete ') ;
    {$I-}
    readln(n) ;
    if ioresult > 0 then
    n := -1 ;
    {$I+}
    assign(f, FILENAME) ;
    open(f) ;
    if (n >= 0) and (n < filesize(f)) then begin
    seek(f,n) ;
    read(f, student) ;
    writeln ;
    writeln ;
    write(BLANK:15, 'Are you sure you want to delete record# ', n:0, '?') ;
    repeat
    ch := upcase(readkey)
    until ch in ['N', 'Y'] ;
    if ch = 'Y' then
    delete_rec(f,n) ;
    end
    else begin
    write(BLANK:15, 'record # ', n:0, ' does not exist') ;
    readln
    end ;
    close(f)
    end ; { del }
    [/code]
    The procedure asks for the number of the record to be deleted, retrieves that record from the file, displays it and then asks for confirmation that you want it deleted.

    The [b]open[/b] procedure for [b]del[/b] attempts to reset the file and opens the bit bucket if it fails. If the record you specify is out of range it tells you so.

    A record is deleted by copying the file to a scratch file, skipping the record to be deleted. Afterward the file is erased and the scratch file renamed.

    The procedure is crude in that it asks the operator for a record number, requiring the operator to find the record via [b]view[/b] and either remembering it or writing it down. A good enhancement would be to be able to search the file for the required record, for example, by searching for an [b]id[/b] number. For this to work [b]app[/b] would need to be modified to prevent duplicate records with the same [b]id[/b].

    Next post will be the code for the [b]edit[/b] procedure.

  • ActorActor Posts: 438Member
    Here is the code for the [b]del[/b] procedure.
    [code]
    procedure del ;
    {
    deletes a record from the file
    }
    procedure open (var f : file_of_studenttype) ;
    {
    opens file for possible deletion of records
    }
    begin { open }
    {$I-}
    reset(f) ;
    if ioresult > 0 then begin
    assign(f, 'NUL') ; { the bit bucket }
    reset(f)
    end
    {$I+}
    end ; { open }

    procedure delete_rec (var f : file_of_studenttype ; n : longint) ;
    {
    delete designated record
    }
    procedure open (var g : file_of_studenttype ; var success : boolean) ;
    {
    open a scratch file
    }
    begin { open }
    {$I-}
    rewrite(g) ;
    success := (ioresult = 0)
    {$I+}
    end ; { open }
    var
    student : studenttype ;
    g : file_of_studenttype ;
    i : longint ;
    success : boolean ;
    begin { delete_rec }
    assign(g, SCRATCH) ;
    open(g, success) ;
    if success then begin
    for i := 0 to filesize(f) - 1 do begin
    seek(f,i) ;
    read(f,student) ;
    if i <> n then
    write(g, student)
    end
    end
    else begin
    writeln(BLANK:15, 'unable to create scratch file ') ;
    readln ;
    end ;
    {$I-}
    close(g) ;

    erase(f) ;
    rename(g, FILENAME)
    {$I+}
    end ; { delete_rec }

    var
    n : longint ;
    f : file_of_studenttype ;
    student : studenttype ;
    ch : char ;
    begin { del }
    clrscr ;
    gotoxy(1,12) ;

    write(BLANK:15, 'Enter the number of the record you wish to delete ') ;
    {$I-}
    readln(n) ;
    if ioresult > 0 then
    n := -1 ;
    {$I+}
    assign(f, FILENAME) ;
    open(f) ;
    if (n >= 0) and (n < filesize(f)) then begin
    seek(f,n) ;
    read(f, student) ;
    writeln ;
    writeln ;
    write(BLANK:15, 'Are you sure you want to delete record# ', n:0, '?') ;
    repeat
    ch := upcase(readkey)
    until ch in ['N', 'Y'] ;
    if ch = 'Y' then
    delete_rec(f,n) ;
    end
    else begin
    write(BLANK:15, 'record # ', n:0, ' does not exist') ;
    readln
    end ;
    close(f)
    end ; { del }
    [/code]
    The procedure asks for the number of the record to be deleted, retrieves that record from the file, displays it and then asks for confirmation that you want it deleted.

    The [b]open[/b] procedure for [b]del[/b] attempts to reset the file and opens the bit bucket if it fails. If the record you specify is out of range it tells you so.

    A record is deleted by copying the file to a scratch file, skipping the record to be deleted. Afterward the file is erased and the scratch file renamed.

    The procedure is crude in that it asks the operator for a record number, requiring the operator to find the record via [b]view[/b] and either remembering it or writing it down. A good enhancement would be to be able to search the file for the required record, for example, by searching for an [b]id[/b] number. For this to work [b]app[/b] would need to be modified to prevent duplicate records with the same [b]id[/b].

    Next post will be the code for the [b]edit[/b] procedure.

«1345
Sign In or Register to comment.