954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pascal code not right help sought

I'm trying to understand Pascal. This code does not work!

does not allow me to enter data, using debug seems to go from where I've indicated (procedure createdir) then jumps to end of main program.

Can any person shed light on it. P.S. I would put it as attachment but type is not one of the allowed sorts.

The main program is below the unit.

unit dir_structure;

interface
   type
      name=string;
      phone=integer;
      link=^node_record;
      node_record=record
                  employee_name:name;
                  telephone:phone;
                  next:link
                  end;

      directory=record
                dir_head:link
      end;

      procedure createdir(var d:directory);
      procedure add(n:name;
                    t:phone;
                    var d:directory);
      procedure remove(searchname:name;
                        var d:directory);
      procedure retrieve (searchname:name;
                         d:directoryunit dir_structure;

interface
   type
      name=string;
      phone=integer;
      link=^node_record;
      node_record=record
                  employee_name:name;
                  telephone:phone;
                  next:link
                  end;

      directory=record
                dir_head:link
      end;

      procedure createdir(var d:directory);
      procedure add(n:name;;
                         t:phone);

      procedure display(d:directory);
      function isin(searchname:name;
      d:directory):Boolean;

implementation
procedure createdir;
begin

         d.dir_head.employee_name:='zzzz'; ***this is where it bombs out!!
         d.dir_head.telephone:=0;
         d.dir_head.next:=nil;


       end;

  procedure add;
   var
       previous,current,temporary:link;
       found:Boolean;

       begin
         previous:=d.dir_head;
         current:=previous^.next;
         found:=(current=nil);
         while (not found) do
         begin
           if n < current^.employee_name
           then
              found:=true
           else
              begin
                  previous:=current;
                  current:=current^.next;
                  found:=(current=nil)
              end

         end;
         if previous^.employee_name=n then
           writeln('action undefined')
           else
             begin


                 new(temporary);
                 temporary^.employee_name:=n;
                 temporary^.telephone:=t;
                 temporary^.next:=current;
                 previous^.next:=temporary
             end;

    end;
   procedure remove;
   var
     previous, current:link;
     begin
       previous:=d.dir_head;
       current:=previous^.next;
       while (current<> nil) and (searchname > current^.employee_name) do
         begin
           previous:=current;
           current:=current^.next
         end;
     end;
  procedure display;
  var
    current:link;
    begin
      current:=d.dir_head^.next;
      while(current<>nil) do
        begin
          writeln(current^.employee_name,current^.telephone);
          current:=current^.next
        end;
    end;
    procedure retrieve;
    var
       current:link;
    begin
      current:=d.dir_head.next;
      while (current<> nil) and (searchname > current^.employee_name) do
         current:=current^.next;
         if searchname<> current^.employee_name
            then
               writeln('action undefined')
            else
               t:=current^.telephone
    end;

    function isin;
    var
       current:link;
       found:Boolean;
    begin
       current:=d.dir_head.next;
       found:=false;
       while (current<> nil) and (not found) do
         if searchname = current^.employee_name
         then
            found:=true
         else
            current:=current^.next;
     isin:=found
     end;


end.


Main program_________________________________________________________________________
program update_directory;

{$APPTYPE CONSOLE}

uses
  SysUtils,
  dir_structure in 'dir_structure.pas';

var
    staff:directory;
    person:name;
    phone_no:phone;
        (*
      var f:text;
    *)
    procedure emp_in( var n:name;
                    var t:phone);

     begin

        write('enter name: ');  readln(n);
        write('enter phone: ');read(t)

     end;
begin

   createdir(staff);
   emp_in(person,phone_no);
   while (person <> 'zzz') do
     begin
        if isin(person, staff)
          then
             writeln('error-name already exists')
          else
             add(person,phone_no,staff);
     emp_in(person,phone_no)

  end;

end.
whitefly
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

Because d.headlink is a pointer to a record, and has not been created with New.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Thanks for your speedy reply. I didn't think I required 'new' in procedure createdir as the first item is a dummy record. I use new to allocate space in procedure 'add'.

Please can you explain a bit more.

whitefly
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

In add you do it right: use New and then set employee name etc. In createdir you try the same with creating memory for a new record, so the headlink points to nothing, hence the crash.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Thanks very much for your explanation. Guess that also explains why I couldn't use with.
As

with d.dir_head do
  begin
    employee_name:='zzzz';
    telephone:=0;
    next:=nil
  end;


this did not compile was it related to the 'new' problem?

whitefly
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

No, I think if you use:

with d.dir_head^ do

it will compile.

pritaeas
Posting Expert
Moderator
5,480 posts since Jul 2006
Reputation Points: 653
Solved Threads: 874
 

Thanks - it works as intended now.

whitefly
Newbie Poster
6 posts since Jul 2011
Reputation Points: 10
Solved Threads: 0
 

This question has already been solved

Post: Markdown Syntax: Formatting Help
You