Languages :: Pascal :: passing pointer to a procedure |
|||
| By: VB guy |
Date: 06/01/2003 00:00:00 |
Points: 50 | Status: Answered Quality : Excellent |
|
let's say u have a pointer pointing to a record which stores info entered by the user. all the info can be easily accessed with use of linked lists, but how would u pass this data (the record) to a procedure? if it helps, below is my incomplete program where im trying to implement this. the idea is that once all the user-entered data has been collected, it will copy all the info to a DAT file, output the info as entered by user, and then move onto one of two procedures. in either one, i need to access the records stored in the main program, but dont know how to do this. program linksys1; uses crt; type CustPtr = ^CustRec; CustRec = record FName, LName : string[13]; phone, fax : string[12]; address : string[20]; city, prov, pcode : string[15]; Prev, Next : CustPtr; end; {---------------------------------------------------------------------------} procedure postalcodes (first,current,prev : custptr; var fhfile:text); var found : custptr; begin new (first); current := first; writeln ('YAAAA'); writeln (current^.fname); writeln (current^.lname); readkey; end; {---------------------------------------------------------------------------} procedure areacodes (first,current,prev : custptr; var fhfile:text); begin end; {---------------------------------------------------------------------------} var first,current,prev : CustPtr; creply : char; fhfile : text; begin clrscr; assign (fhfile, 'ClientInfo.dat'); rewrite (fhfile); append (fhfile); first := nil; repeat clrscr; if (first=nil) then begin new (first); current := first; write ('Enter the first name: '); readln (current^.fname); write ('Enter the last name: '); readln (current^.lname); write ('Enter the phone #: '); readln (current^.phone); write ('Enter the fax #: '); readln (current^.fax); write ('Enter the street address: '); readln (current^.address); write ('Enter the city name: '); readln (current^.city); write ('Enter the province name: '); readln (current^.prov); write ('Enter the postal code: '); readln (current^.pcode); end else begin prev := current; new (current^.next); current := current^.next; write ('Enter the first name: '); readln (current^.fname); write ('Enter the last name: '); readln (current^.lname); write ('Enter the phone #: '); readln (current^.phone); write ('Enter the fax #: '); readln (current^.fax); write ('Enter the street address: '); readln (current^.address); write ('Enter the city name: '); readln (current^.city); write ('Enter the province name: '); readln (current^.prov); write ('Enter the postal code: '); readln (current^.pcode); new (current^.next); current^.next := nil; end; writeln(''); write('Are there any more customers? (y/n)'); readln (creply); until (creply='n') or (creply='N'); {Write to file} current := first; repeat writeln (fhfile, current^.fname); writeln (fhfile, current^.lname); writeln (fhfile, current^.phone); writeln (fhfile, current^.fax); writeln (fhfile, current^.address); writeln (fhfile, current^.city); writeln (fhfile, current^.prov); writeln (fhfile, current^.pcode); writeln (fhfile, ''); current := current^.next; until (current=nil); writeln (fhfile, 'ENDCHECK'); {Output to screen} current := first; repeat writeln (current^.fname); writeln (current^.lname); writeln (current^.phone); writeln (current^.fax); writeln (current^.address); writeln (current^.city); writeln (current^.prov); writeln (current^.pcode); writeln (''); current := current^.next; until (current=nil); { clrscr; writeln ('Make a choice: '); writeln ('P.) Read through the record and print all records with postal codes starting with L or M'); writeln ('A.) Read through the record and print all records with area codes starting with 705'); write ('Enter your choice: '); readln (creply); if (creply='P') or (creply='p') then begin postalcodes (first, current, prev,fhfile); end else if (creply='A') or (creply='a') then begin areacodes (first, current, prev, fhfile); end; } current := first; writeln ('YAAA'); writeln (current^.fname); writeln (current^.lname); readkey; current := first; {Dispose record} while current^.next <> nil do begin first := current^.next; dispose(current); current := first; end; readkey; close (fhfile); end. any response would be appreciated. thanx. |
|||
| By: monange | Date: 07/01/2003 07:08:00 | Type : Comment |
|
| This might be enough of a start. All it does is print out the postal codes that start with L for you but should give you the idea. Not tested and no guarantee. Oh, and inside a procedure try and make the names of any parameters inside a procedure a different name to that passed to it, eg procedure process_data(first_no, second_no : integer); begin total := first_no + second_no; end; and when you call it. process_data(number_one, number_two : integer); procedure postalcodes (first : custptr; ); begin while first<> nil do begin if first^.pcode[1] = 'L' then begin writeln (first^.fname); writeln (first^.lname); writeln (first^.phone); writeln (first^.fax); writeln (first^.address); writeln (first^.city); writeln (first^.prov); writeln (first^.pcode); writeln (''); end; first := first^.next; end; end; |
|||
| By: VGR | Date: 08/01/2003 07:59:00 | Type : Comment |
|
| note also that "first" is not disposed of in your code above. Then I would also pass the record variable's reference as a VAR parameter (just good practice, no impact ont he code I guess) And last, I already use records' files all the time. You should use a file of typerecord and just do Write(fileof,yourrecord) and read(fileof, yourrecord) it's easier |
|||
| By: VGR | Date: 08/01/2003 20:02:00 | Type : Answer |
|
| correction to the above : of course as you have a linked list, you can't just write records the way I wrote. Stick with your loops. You could still use a file of recordtype, write each record wholly, but need to have an indicator "this is the follow-up of the current list", and you would ignore pointers' values when reading back |
|||
|
Do register to be able to answer |
|||
©2010 These pages are served without commercial sponsorship. (No popup ads, etc...). Bandwidth abuse increases hosting cost forcing sponsorship or shutdown. This server aggressively defends against automated copying for any reason including offline viewing, duplication, etc... Please respect this requirement and DO NOT RIP THIS SITE.
Please DO link to this page!








