Procedure for deleting array content

Please support our Pascal and Delphi advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Dec 2007
Posts: 7
Reputation: simps0n is an unknown quantity at this point 
Solved Threads: 0
simps0n simps0n is offline Offline
Newbie Poster

Procedure for deleting array content

 
0
  #1
Dec 5th, 2007
Hello, guys!

I have to make a program with the following features.
- array of records
  1. TYPE
  2. employeeRecord = RECORD
  3. name: STRING;
  4. surname: STRING;
  5. age: Integer;
  6. END;
  7. employeeRecords = ARRAY OF employeeRecord ;
- a procedure/function to ADD data into the array
I did this one and I think it is working fine!
- a procedure/function to DELETE data from the array
I have problems with this procedure. It doesn't work when I try to delete the data in the first position of the array. Here's what I did so far:
  1. PROCEDURE delete(VAR records: employeeRecords);
  2. VAR
  3. n, j: Integer;
  4. nameDel: STRING;
  5.  
  6. BEGIN
  7. Write('Name: ');
  8. Readln(nameDel);
  9.  
  10. n := 0;
  11.  
  12. WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
  13. BEGIN
  14. inc(n);
  15. END;
  16.  
  17. IF records[n].name <> nameDel THEN
  18. BEGIN
  19. Writeln('That employee could not be found!');
  20. Writeln;
  21. END;
  22.  
  23. IF records[n].name = nameDel THEN
  24. BEGIN
  25. FOR j := n TO High(records)-1 DO
  26. BEGIN
  27. records[j].name := records[j+1].name;
  28. records[j].surname := records[j+1].surname;
  29. records[j].age:= records[j+1].age;
  30. SetLength(records, High(records)); // MAYBE THIS LINE IS NOT CORRECT
  31. END
  32. END
  33.  
  34. END;
If you have some ideas, please let me know. Thanks in advance!
Last edited by simps0n; Dec 5th, 2007 at 12:02 pm.
Reply With Quote Quick reply to this message  
Join Date: Oct 2007
Posts: 1,953
Reputation: Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of Duoas has much to be proud of 
Solved Threads: 214
Featured Poster
Duoas's Avatar
Duoas Duoas is offline Offline
Posting Virtuoso

Re: Procedure for deleting array content

 
0
  #2
Dec 5th, 2007
The SetLength should not be in the loop, but after it. Other than that it looks fine to me.

BTW. You should be able to replace lines 27..29 with just
records[j] := records[j+1];

Don't know why you are using all-caps keywords, but it sure is anachronistic (a hold-over from brain-damaged BASIC users). I've not changed the functionality of your code here. Just structured it a little better...
  1. PROCEDURE delete(VAR records: employeeRecords);
  2. VAR
  3. n, j: Integer;
  4. nameDel: String;
  5.  
  6. BEGIN
  7. Write('Name: ');
  8. Readln(nameDel);
  9.  
  10. n := 0;
  11.  
  12. WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
  13. Inc(n);
  14.  
  15. IF records[n].name = nameDel
  16. THEN BEGIN
  17. FOR j := n TO High(records)-1 DO
  18. records[j] := records[j+1];
  19. SetLength(records, High(records))
  20. END
  21. ELSE BEGIN
  22. Writeln('That employee could not be found!');
  23. Writeln
  24. END
  25. END;
Hope this helps.
Reply With Quote Quick reply to this message  
Join Date: Dec 2007
Posts: 7
Reputation: simps0n is an unknown quantity at this point 
Solved Threads: 0
simps0n simps0n is offline Offline
Newbie Poster

Re: Procedure for deleting array content

 
0
  #3
Dec 5th, 2007
Thank you very much, Duoas! Much appreciated.
You were perfectly clear and really helped me a lot.

Originally Posted by Duoas
Don't know why you are using all-caps keywords, ...
I'm sorry for that.
Last edited by simps0n; Dec 5th, 2007 at 4:48 pm.
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Pascal and Delphi Forum
Thread Tools Search this Thread



About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC