Hello, guys!

I have to make a program with the following features.
- array of records

TYPE
  employeeRecord  = RECORD
                   name: STRING;
                   surname: STRING;
                   age: Integer;
                   END;
  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:

PROCEDURE delete(VAR records: employeeRecords);
VAR
  n, j: Integer;
  nameDel: STRING;

BEGIN
  Write('Name: ');
  Readln(nameDel);

  n := 0;

  WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
  BEGIN
    inc(n);
  END;
 
  IF records[n].name <> nameDel THEN
  BEGIN
    Writeln('That employee could not be found!');
    Writeln;
  END;

  IF records[n].name = nameDel THEN
  BEGIN
    FOR j := n TO High(records)-1 DO
    BEGIN
      records[j].name := records[j+1].name;
      records[j].surname := records[j+1].surname;
      records[j].age:= records[j+1].age;
      SetLength(records, High(records)); // MAYBE THIS LINE IS NOT CORRECT
    END
  END

END;

If you have some ideas, please let me know. Thanks in advance!

Recommended Answers

All 2 Replies

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...

PROCEDURE delete(VAR records: employeeRecords);
VAR
  n, j: Integer;
  nameDel: String;

BEGIN
  Write('Name: ');
  Readln(nameDel);

  n := 0;

  WHILE (records[n].name <> nameDel) AND (n < High(records)) DO
    Inc(n);
 
  IF records[n].name = nameDel
    THEN BEGIN
           FOR j := n TO High(records)-1 DO
             records[j] := records[j+1];
           SetLength(records, High(records))
         END
    ELSE BEGIN
           Writeln('That employee could not be found!');
           Writeln
         END
END;

Hope this helps.

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

Don't know why you are using all-caps keywords, ...

I'm sorry for that.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.