1

I'm currently having trouble with these errors and can't seem to get through them, I've attached below my errors as well as my code, thank you.

Errors:

Free Pascal Compiler version 2.6.4 [2014/02/26] for i386 Copyright (c) 1993-2014 by Florian Klaempfl and others Target OS: Darwin for i386 Compiling BasicReadWrite.pas BasicReadWrite.pas(22,30) Error: Incompatible types: got "personArray" expected "LongInt" BasicReadWrite.pas(25,8) Error: No default property available BasicReadWrite.pas(25,8) Fatal: Syntax error, ";" expected but "[" found Fatal: Compilation aborted Error: /usr/local/bin/ppc386 returned an error exitcode (normal if you did not specify a source file to be compiled)

program BasicReadWrite;

type

  Person = record
        name: String;
        age: String;  // Should this be an integer? Why/Why not?
  end;

 personArray = array of Person;

procedure WriteLinesToFile(var myFile: TextFile; const pe: Person);
begin
    WriteLn(myFile, pe.age);
    WriteLn(myFile, pe.name);
end;

procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
  p: Person;
begin
  setLength(p, ArrayOfPersons);
  for count:= 0 to high(ArrayOfPersons) do 
    begin
         p[count] := WriteLinesToFile();
    end;
end;

procedure ReadLinesFromFile(var myFile: TextFile);
var 
  p: Person;
  number: Integer;
  ArrayOfPersons: personArray;
begin
  for number:= 0 to 20 do 
    begin
       PrintRecords([number]);
    end;
end;

procedure Main();
var 
myFile: TextFile;
begin
  AssignFile(myFile, 'mytestfile.dat');
  ReWrite(myFile);  // Use ReWrite to open a file for writing 
  WriteLinesToFile(myFile);
  Close(myFile); // We need to close the file and re-open it, as Pascal
                // will not let you Read and write from a file at the same time.

  AssignFile(myFile, 'mytestfile.dat');
  Reset(myFile); // Open the file for reading.
  ReadLinesFromFile(myFile);
  Close(myFile);
end;

begin
  Main();
end.
1
  • 1
    Your code has many errors. You use wrong types, wrong syntax, your passed parameters do not always match the declared parameters, etc. The compiler can't make anything of this. E.g. in ReadLinesFromFile you don't read anything from a file. In PrintRecords you actually don't print records either, and use wrong types, you pass wrong parameters to SetLength, and it is totally unclear why you want to use an array at all. One gets the impression you don't really know what you are doing. Ask a teacher or tutor or whomever to help you. Commented May 29, 2018 at 18:04

2 Answers 2

3

Usually the first error is the one to focus on first. The following errors may just be consequences of the first one. I will help you get started with the first error, but leave the rest for you to solve. You may want to discuss with your tutor about the errors.

So, first focus on

BasicReadWrite.pas(22,30) Error: Incompatible types: got "personArray" expected "LongInt"

Line 22 is in

procedure PrintRecords(const ArrayOfPersons: personArray; count: Integer);
var
  p: Person;
begin
  setLength(p, ArrayOfPersons); // line 22

That line is erroneous, because:

  1. p is a Person type record. You can not set the length of a record.
  2. The second argument to SetLength() needs to be a an integer. ArrayOfPersons is not an integer.

I don't see any reason to set the length of anything in that procedure.

Sign up to request clarification or add additional context in comments.

Comments

1

Just in case you need some explanations, when you declare:

personArray = array of Person;

it means personArray is a dynamic array. First you'll need to specify the length of this dynamic array before use, e.g:

setlength(personArray,20);

where 20 is the amount of indexes to be stored in personArray (Don't forget first index is 0!). e.g:

personArray[0].name:= 'John';

age: String; // Should this be an integer? Why/Why not?

Definitely yes, age SHOULD be an integer. You can store an age in a String, but an integer takes less memory and you can use math operations with integers (What if you need add or substract ages? You can't do math operations with strings). String should be used only when you need to store alphanumerical data.

Good luck!

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.