# Board index » delphi » Pascal-problems

## Pascal-problems

I would be very grateful if someone could help me out with this one.
I've a program were the user should put in data about dogs, and then be
able to write them out on the screen.

PROGRAM inlu5(Input, Output);
TYPE String_15=packed ARRAY[1..15] of CHAR;

hundpekare =^hundtyp;

hundtyp=record
namn  : String_15;
ras   : String_15;
age   : Integer;
vikt  : Integer;
next  : hundpekare;
END;

VAR       first,ny,temp : hundpekare;
continue : Boolean;
kom : Char;
i,antal : Integer;

BEGIN
Writeln('Welcome to dogprogram!');

new(first);
first^.next:=NIL;    {pekaren nollst?lls}
Write('Name of dog?');  Readln(first^.namn);
Write('Race of dog?');  Readln(first^.ras);
Write('Age of dog?');Readln(first^.age);
Write('Weight of dog?'); Readln(first^.vikt);

continue:=true;
Repeat
BEGIN
Write('Do you want to put in data about another dog?Y/N');
Readln(kom);
CASE kom OF
'Y','y':   BEGIN

new(temp);
temp:=first;
Writeln(first^.namn:5,':',first^.ras:5,':',first^.age:2,' first^.');
Writeln(temp^.namn:5,':',temp^.ras:5,':',temp^.age:2,' temp^.');

repeat
BEGIN
Writeln('Loop!');
temp:=temp^.next;
END;
until (temp^.next=NIL);

Write('Name?'); Readln(temp^.next^.namn);
Write('Race?');  Readln(temp^.next^.ras);
Write('Age?');Readln(temp^.next^.age);
Write('Weight?'); Readln(temp^.next^.vikt);
temp:=NIL;
END;
'N','n': continue:=false;
Otherwise
BEGIN
Writeln('Wrong kommando, try again!');
continue:=true;
END;
END;
END;
Until NOT continue;

Writeln(antal);
Write('Do you want to write the register? Y/N:');
Readln(kom);
Case kom of
'Y','y': BEGIN
{ I dont know how to make this correct}
END;
'N','n': Writeln('Bye');
Otherwise Writeln('Wrong kommando. Bye!');
END;
END.

## Re:Pascal-problems

I think you should be more specific about how far you got and what
your particular problems are.  Most of us haven't got time to read
code to try to figure out what you're doing and why.

It often helps to create a minimum program and expand it later.  For
instance, don't work with large records and multiple elements.  Start
with a single data field and a link field and work with that until your
functions work correctly.  Then adding additional fields should be
a piece of cake.

Rufus

##### Quote
"Pontus Rosin" <hme01...@syd.kth.se> wrote in message

news:aWr89.5858\$6t5.3230964@news01.chello.se...
##### Quote
> I would be very grateful if someone could help me out with this one.
>  I've a program were the user should put in data about dogs, and then be
> able to write them out on the screen.

## Re:Pascal-problems

"Pontus Rosin" <hme01...@syd.kth.se> wrote in news:aWr89.5858\$6t5.3230964
@news01.chello.se

instead of making one huge main program try dividing the problem into
smaller parts... I would write the following procedures:

procedure ReadDogInfo(pDog : hundpekare)
procedure PrintDogInfo(pDog : hundpekare)

and perhaps even these

procedure PrintAllDogs(pFirstDog : hundpekare)
procedure DeleteDogs(pFirstDog : hundpekare)

Try to focus on one procedure at a time, don't use global variables in
the procedures and indent your code and this assignment should be a piece
of cake :) (I am assuming this is an assignment, if it's not please feel
free to abuse, flame and throw bad words at me)

/Markus

--
to replay replace the following...
first=Markus, last=Edholm, isp = telia

## Re:Pascal-problems

"Pontus Rosin" <hme01...@syd.kth.se> schreef in bericht
news:aWr89.5858\$6t5.3230964@news01.chello.se...

##### Quote
> I would be very grateful if someone could help me out with this one.
>  I've a program were the user should put in data about dogs, and then
be
> able to write them out on the screen.

You should bring a little structure in your program. Use functions and
procedures. Avoid global variabeles.

Success

--
Femme

PROGRAM inlu5(Input, Output);
uses crt;           {needed for the readkey function instead of read or
readln}

##### Quote
> TYPE String_15=packed ARRAY[1..15] of CHAR;

You may not be able to read or write variabeles of this
type
TYPE String_15=string[15];   will probably work

hundpekare =^hundtyp;

hundtyp=record
namn  : String_15;
ras   : String_15;
age   : Integer;
vikt  : Integer;
next  : hundpekare;
END;

Procedure adddogtolist(Var TheList:hundpekare);
{note that by putting Var in the header TheList is using the
actual pointer variabele}
var temp:hundpekare;
begin
new(temp);
temp^.next:=thelist;
thelist:=temp;   {note that the first element in the list is the last
one added}
with temp^ do
begin
Write('Name?');   Readln(namn);
Write('Race?');   Readln(ras);
Write('Age?') ;   Readln(age);
Write('Weight?'); Readln(vikt);
end;
end;

procedure displayentirelist(TheList:hundpekare);
{note TheList is a copy of the pointer variabele}
begin
while thelist<>nil do
begin
with thelist^ do writeln (namn, ras,age, vikt);
thelist:=thelist^.next;
end;
writeln ('press any key');
readkey;
end;

function count(thelist:hundpekare):integer;
var I:integer;
begin
i:=0;
while thelist<>nil do
begin
inc(i);
thelist:=thelist^.next;
end;
count:=I
end;

Write your own procedures here
e.g. sort the list
delete an item
safe the list to a file
reload a previously saved list

VAR   first : hundpekare;

BEGIN
first:=nil;  {start with an empty list}
Repeat
clrscr;
Writeln('Welcome to the doggy doggy program!');
writeln('the list contains ',count(first),' items');
writeln ('A:   add a dog to the list');
writeln ('D:   display dog list');
.....E:...
.....F:

writeln ('Q:   quit program');
case Upcase(readkey) of
'A':adddogtolist(first);
'D':displayentirelist(first);
.....E:...
.....F:....

'Q':halt;             {program exits here}
else write(#7);   {beep in case of wrong key}
end;  {case}
until false;
end.

## Re:Pascal-problems

hi

as others already wrote, try to structure your program, to make it easier to read and maintain.

"Pontus Rosin" <hme01...@syd.kth.se> schrieb

##### Quote
> I would be very grateful if someone could help me out with this one.
>  I've a program were the user should put in data about dogs, and then be
> able to write them out on the screen.

> PROGRAM inlu5(Input, Output);
> TYPE String_15=packed ARRAY[1..15] of CHAR;

>      hundpekare =^hundtyp;

>      hundtyp=record
>         namn  : String_15;
>         ras   : String_15;
>         age   : Integer;
>         vikt  : Integer;
>         next  : hundpekare;
>      END;

> VAR       first,ny,temp : hundpekare;
>                continue : Boolean;
>                   kom : Char;
>               i,antal : Integer;

> BEGIN
>   Writeln('Welcome to dogprogram!');

>   new(first);
>   first^.next:=NIL;    {pekaren nollst?lls}
>  Write('Name of dog?');  Readln(first^.namn);
>     Write('Race of dog?');  Readln(first^.ras);
>     Write('Age of dog?');Readln(first^.age);
>     Write('Weight of dog?'); Readln(first^.vikt);

>   continue:=true;
>   Repeat
>   BEGIN
>   Write('Do you want to put in data about another dog?Y/N');
>  Readln(kom);
>          CASE kom OF
>           'Y','y':   BEGIN

>  new(temp);
>  temp:=first;

this does not make sense. you're reserving space for temp^. but when assigning 'temp:=first' you are loosing the space again and you cannot even release it anymore.

1. delete the line new(temp)!

##### Quote
> Writeln(first^.namn:5,':',first^.ras:5,':',first^.age:2,' first^.');
> Writeln(temp^.namn:5,':',temp^.ras:5,':',temp^.age:2,' temp^.');

>  repeat
> BEGIN
>      Writeln('Loop!');
>     temp:=temp^.next;
> END;
> until (temp^.next=NIL);

ok. now look at the next line.

##### Quote
> Write('Name?'); Readln(temp^.next^.namn);

if temp^.next = nil ... this readln(temp^.next^.) should cause your program to crash.

what you probably wanted ...

...
until (temp^.next=NIL);
new(temp^.next); { add this lines }
temp^.next^.next := nil { the new end of the list }
Write('Name?'); Readln(temp^.next^.namn);
...

it is always a good idea to take a piece of paper first if you are new in working with pointers.

do you have to put the new dogs at the end of the list? if so, it might be very useful to keep track of the last element in the list.

hth
jochen

- Show quoted text -

##### Quote
> Write('Race?');  Readln(temp^.next^.ras);
> Write('Age?');Readln(temp^.next^.age);
> Write('Weight?'); Readln(temp^.next^.vikt);
> temp:=NIL;
> END;
> 'N','n': continue:=false;
> Otherwise
>   BEGIN
>   Writeln('Wrong kommando, try again!');
>        continue:=true;
>   END;
>   END;
>   END;
> Until NOT continue;

>   Writeln(antal);
>   Write('Do you want to write the register? Y/N:');
>   Readln(kom);
>   Case kom of
>    'Y','y': BEGIN
> { I dont know how to make this correct}
>     END;
>    'N','n': Writeln('Bye');
>    Otherwise Writeln('Wrong kommando. Bye!');
>   END;
>  END.

## Re:Pascal-problems

##### Quote

>>  new(temp);
>>  temp:=first;

>this does not make sense. you're reserving space for temp^. but when

assigning 'temp:=first' you are loosing the space again and you >cannot
even release it anymore.

##### Quote

>1. delete the line new(temp)!

Perhaps what he meant is

new(temp);
temp^.next:=first;
first:=temp;

This way you get each new item inserted as first element in the list.

##### Quote
>ok. now look at the next line.

>> Write('Name?'); Readln(temp^.next^.namn);

>if temp^.next = nil ... this readln(temp^.next^.) should cause your
program to crash.

>what you probably wanted ...

>...
>until (temp^.next=NIL);
>new(temp^.next); { add this lines }

temp^:=temp^.next;   {move the temp pointer to the last element in the
list}
temp^.next := nil { the new end of the list }
Write('Name?'); Readln(temp^.namn);
...
##### Quote
>it is always a good idea to take a piece of paper first if you are new

in working with pointers.
##### Quote

Fully agree on that. Especially linked lists are quite tricky.

##### Quote
>do you have to put the new dogs at the end of the list? if so, it might

be very useful to keep track of the last element in the list.

--
Femme

## Re:Pascal-problems

hi femme,

"Femme Verbeek" schrieb

##### Quote

> >>  new(temp);
> >>  temp:=first;

> >this does not make sense. you're reserving space for temp^. but when
> >assigning 'temp:=first' you are loosing the space again and you
> >cannot even release it anymore.

> >1. delete the line new(temp)!

> Perhaps what he meant is

> new(temp);
> temp^.next:=first;
> first:=temp;

> This way you get each new item inserted as first element in the list.

might be. but the rest of the op's algorithm points me to another direction.
of course, adding in front of the list, is the best method in this case.

##### Quote
> >what you probably wanted ...
[...]
> >until (temp^.next=NIL);
> >new(temp^.next); { add this lines }

> temp^:=temp^.next;   {move the temp pointer to the last element in the
> list}

ups. i should have taken a piece of paper too. ;-)

##### Quote
> temp^.next := nil { the new end of the list }
> Write('Name?'); Readln(temp^.namn);

[...]

best regards
jochen