943,884 Members | Top Members by Rank

Ad:
Dec 5th, 2007
0

Procedure for deleting array content

Expand Post »
Hello, guys!

I have to make a program with the following features.
- array of records
Pascal Syntax (Toggle Plain Text)
  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:
Pascal Syntax (Toggle Plain Text)
  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.
Similar Threads
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simps0n is offline Offline
7 posts
since Dec 2007
Dec 5th, 2007
0

Re: Procedure for deleting array content

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...
Pascal Syntax (Toggle Plain Text)
  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.
Featured Poster
Reputation Points: 1140
Solved Threads: 229
Postaholic
Duoas is offline Offline
2,039 posts
since Oct 2007
Dec 5th, 2007
0

Re: Procedure for deleting array content

Thank you very much, Duoas! Much appreciated.
You were perfectly clear and really helped me a lot.

Quote 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.
Reputation Points: 10
Solved Threads: 0
Newbie Poster
simps0n is offline Offline
7 posts
since Dec 2007

This thread is more than three months old

No one has posted to this discussion for at least three months. Please let old threads die and do not reply to them unless you feel you have something new and valuable to contribute that absolutely must be added to make the discussion complete. Otherwise, please start a new thread in this forum instead.
Message:
Previous Thread in Pascal and Delphi Forum Timeline: trying to use one form to open another form
Next Thread in Pascal and Delphi Forum Timeline: POSIBLE, POP3 indy using port 995





About Us | Contact Us | Advertise | Acceptable Use Policy
Forum Index | Build Custom RSS Feed


Follow us on Twitter


© 2011 DaniWeb® LLC