### Bookstore

 Computer Math and Games in Pascal (preview) Lazarus Handbook

### Author Topic: I am the new of pascal .please give me some HELP!  (Read 16075 times)

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #15 on: October 10, 2016, 02:03:23 pm »
.
7vinbaby

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #16 on: October 10, 2016, 02:03:39 pm »
I haven't found the culprit yet but i did run into something else.

What happens if the file airport_condition.txt is empty ?
Code: Pascal  [Select][+][-]
1. Procedure input_platform_condition(Var num:integer);
2. Var x,counter: integer;
3. Begin
4.   num := 0;
5.   For x:=1 To 30
6.     Do platform[x].airplane_no := '';
7.   assign(platformfile,'airport_condition.txt');
8.   reset(platformfile);
9.   counter := 0;
10.   While counter<30 Do
11.   Begin
12.     counter := counter+1;
13.     With platform[num] Do
14.     Begin
17.
18.       If (airplane_no<>'\$\$\$')
19.       Then num := num+1;
20.     End;
21.   End;
22.   close(platformfile);
23. End;
24.

The code simply assumes that when the plane is not \$\$\$ that it is a plane, which is wrong in case the statement readln(platformfile,airplane_no); produced an empty airplane_no.

Fix that by:
Code: [Select]
`If ( (airplane_no<>'\$\$\$') and (airplane_no <> '') )      Then num := num+1;`As i assume it is allowed to have no planes at all at the airport (and only have planes in the air, waiting for landing).

And that leads to another problem, namely that you start-loop is stuck when this condition happens

Because if there are no planes at the airport, the routine input_platform_condition returns the num variable (which is then zero).

Take a look at your loop:
Code: Pascal  [Select][+][-]
1.   While num>=0 Do
2.   Begin
3.     // blah
4.     Case ch Of
5.       #13:
6.       Begin
7.         If (num=0) And (count2=0) Then
8.         begin
9.           // blah
10.         end;
11.
12.         If (num<>0) And (count2<>0) Then
13.         Begin
14.           // blah
15.         End;
16.       End;
17.   End;
18.

Num is the number of planes at the airport (which is zero), and count2 is the number of planes in the air (three given your example file).

None of these conditions is met and, the loop will go on forever without actually doing anything.

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #17 on: October 10, 2016, 02:25:14 pm »
I haven't found the culprit yet but i did run into something else.

What happens if the file airport_condition.txt is empty ?
Code: Pascal  [Select][+][-]
1. Procedure input_platform_condition(Var num:integer);
2. Var x,counter: integer;
3. Begin
4.   num := 0;
5.   For x:=1 To 30
6.     Do platform[x].airplane_no := '';
7.   assign(platformfile,'airport_condition.txt');
8.   reset(platformfile);
9.   counter := 0;
10.   While counter<30 Do
11.   Begin
12.     counter := counter+1;
13.     With platform[num] Do
14.     Begin
17.
18.       If (airplane_no<>'\$\$\$')
19.       Then num := num+1;
20.     End;
21.   End;
22.   close(platformfile);
23. End;
24.

The code simply assumes that when the plane is not \$\$\$ that it is a plane, which is wrong in case the statement readln(platformfile,airplane_no); produced an empty airplane_no.

Fix that by:
Code: [Select]
`If ( (airplane_no<>'\$\$\$') and (airplane_no <> '') )      Then num := num+1;`As i assume it is allowed to have no planes at all at the airport (and only have planes in the air, waiting for landing).

And that leads to another problem, namely that you start-loop is stuck when this condition happens

Because if there are no planes at the airport, the routine input_platform_condition returns the num variable (which is then zero).

Take a look at your loop:
Code: Pascal  [Select][+][-]
1.   While num>=0 Do
2.   Begin
3.     // blah
4.     Case ch Of
5.       #13:
6.       Begin
7.         If (num=0) And (count2=0) Then
8.         begin
9.           // blah
10.         end;
11.
12.         If (num<>0) And (count2<>0) Then
13.         Begin
14.           // blah
15.         End;
16.       End;
17.   End;
18.

Num is the number of planes at the airport (which is zero), and count2 is the number of planes in the air (three given your example file).

None of these conditions is met and, the loop will go on forever without actually doing anything.

I have made a correction like this

Code: [Select]
`procedure input_platform_condition(var num:integer);var x,counter,num1:integer;begin        num:=0;        num1:=1;        for x:=1 to 30 do        platform[x].airplane_no:='';        assign(platformfile,'C:/airtrafficcontrol/airport_condition.txt');        reset(platformfile);                while not eof(platformfile) do                begin                with platform[num1] do                begin                        readln(platformfile,airplane_no);                        readln(platformfile,airplane_predicted_time_left);                        num1:=num1+1;                        if airplane_no<>'empty'                        then num:=num+1;                end;                end;close(platformfile);end;`
Is that all right (i change the \$\$\$ to empty)?
7vinbaby

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #18 on: October 10, 2016, 02:39:18 pm »
Is that all right (i change the \$\$\$ to empty)?
Well, the \$\$\$ sign can stay if you want to

It is the while eof that fixes that issue without the need to check if an entry is actually empty.

The real cause for things going wrong is inside routine update_air_traffic;

In there is a piece of code that reads:
Code: Pascal  [Select][+][-]
1.             While target=0 Do
2.             Begin
3.               count1 := count1+1;
4.               If count1>30 Then
5.               begin
6.                 WriteLn('count > 30 so exit. Press enter');
8.                 exit;
9.               end;
10.
11.               If platform[count1].airplane_no='\$\$\$'
12.               Then target := count1;
13.             End;
14.
Gues what i am seeing _all_ the time

Answer: "count > 30 so exit. Press enter"

I have no idea what you had in mind there. Maybe you can correct that yourself using your own preferred condition(s) ?

One thing is for sure and that is that the \$\$\$ cehck alone is not enough... unless it is mandatory to fill up the complete airspace first ?

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #19 on: October 10, 2016, 02:56:47 pm »
Code: [Select]
`while (target=0) and (count1>31) do                                                        begin                                                                count1:=count1+1;                                                                if platform[count1].airplane_no='empty'                                                                then target:=count1;                                                        end;`
Is that better?
7vinbaby

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #20 on: October 10, 2016, 03:21:54 pm »
Is that better?
Except for the fact that entries inside file airport_condition are still being written with empty airplane_no's.

Other then that, somewhere around entry 20 things go wrong inside file airtraffic.txt as well (empty lines for both airplane_no and the left time). Somewhere in at the end i even got entries reading -78117079 and 2088 for airplane_no and an empty line for time_left

Can't seem to get my head around why the empty entries inside file airport_condition are being created.

PS: oops, no. Unfortunately that is not going to work 7vinbaby. If you do that then when it happens to be the first plane directly wanting to land the count1 variable will be zero and, you assign that as platfom. You arrays starts counting from one.
« Last Edit: October 10, 2016, 03:33:05 pm by molly »

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #21 on: October 10, 2016, 03:33:37 pm »
Is that better?
Except for the fact that entries inside file airport_condition are still being written with empty airplane_no's.

Other then that, somewhere around entry 20 things go wrong inside file airtraffic.txt as well (empty lines for both airplane_no and the left time). Somewhere in at the end i even got entries reading -78117079 and 2088 for airplane_no and an empty line for time_left

Can't seem to get my head around why the empty entries inside file airport_condition are being created.

You mean the empty entries should not be created?
But i need to know which entrie is empty in order to give to the landing airplane.
7vinbaby

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #22 on: October 10, 2016, 03:37:56 pm »
PS: oops, no. Unfortunately that is not going to work 7vinbaby. If you do that then when it happens to be the first plane directly wanting to land the count1 variable will be zero and, you assign that as platfom. You arrays starts counting from one.

i am sorry that where are the position you are talking about?
7vinbaby

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #23 on: October 10, 2016, 03:43:18 pm »
Can't seem to get my head around why the empty entries inside file airport_condition are being created.
You mean the empty entries should not be created?
But i need to know which entrie is empty in order to give to the landing airplane.
Sure, but your code doesn't act on it. If an entry is empty your current code happily assumes it is a valid plane number. It does not distinguish between them.

PS: oops, no. Unfortunately that is not going to work 7vinbaby. If you do that then when it happens to be the first plane directly wanting to land the count1 variable will be zero and, you assign that as platfom. You arrays starts counting from one.
i am sorry that where are the position you are talking about?

Quote
chosen to allow landing, searching for a free platform
The target platform is 0
The count1 is0

airplane qwe7634153 landing to platform 0
And that is a big no-no for your arays.

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #24 on: October 10, 2016, 03:55:50 pm »
Quote
chosen to allow landing, searching for a free platform
The target platform is 0
The count1 is0

airplane qwe7634153 landing to platform 0

Code: [Select]
`program air_traffic_control;uses Dos,Crt;type        airport_platform=record                         airplane_no:string[30];                         airplane_predicted_time_left:integer;                         end;        airtraffic_condition=record                                airplane_no:string[30];                                airplane_landing_time_left:integer;                             end;var        hour,min,sec,msec:word;        num,target,count,count2,counter,z,a,q:integer;        platformfile,airtrafficfile:text;        platform:array[1..30] of airport_platform;        airtraffic:array[1..20] of airtraffic_condition;        ch:char;procedure user_display;type DayType=(Sun,Mon,Tue,Wed,Thu,Fri,Sat);var        YY,MM,DD,DayOfWeek,hour,min,sec,msec:word;        today:DayType;        password:string[20];begin        textbackground(black);        ClrScr;        Getdate(YY,MM,DD,DayOfWeek);        GetTime(hour,min,sec,msec);        Today:=DayType(DayOfWeek);        textcolor(yellow);        writeln('Welcome to execute air traffic control');        repeat                textcolor(yellow);                write('Password:');                readln(password);                if password<>'aed7689'                        then writeln('Wrong password,enter again');        until password='aed7689';        ClrScr;        writeln('Day of Today:',DD,'/',MM,'/',YY,',',Today);        writeln('Current time: ',hour,':',min,':',sec);        writeln('Now start the system of air traffic control management');        repeat                textcolor(yellow);                write('Enter password again to continue:');                readln(password);                if password<>'aed7689'                        then writeln('Wrong password,enter again');        until password='aed7689';        ClrScr;end;procedure input_airtraffic(var count2:integer);var count,x:integer;begin        for x:=1 to 30 do        airtraffic[x].airplane_no:='';        count2:=0;        assign(airtrafficfile,'C:/airtrafficcontrol/airtraffic.txt');        reset(airtrafficfile);        while not eof(airtrafficfile) do        begin                count2:=count2+1;                with airtraffic[count2] do                begin                        readln(airtrafficfile,airplane_no);                        readln(airtrafficfile,airplane_landing_time_left);                end;        end;close(airtrafficfile);end;procedure input_platform_condition(var num:integer);var x,counter,num1:integer;begin        num:=0;        num1:=1;        for x:=1 to 30 do        platform[x].airplane_no:='';        assign(platformfile,'C:/airtrafficcontrol/airport_condition.txt');        reset(platformfile);                while not eof(platformfile) do                begin                with platform[num1] do                begin                        readln(platformfile,airplane_no);                        readln(platformfile,airplane_predicted_time_left);                        num1:=num1+1;                        if airplane_no<>'empty'                        then num:=num+1;                end;                end;close(platformfile);end;procedure take_off(var num,target:integer);var choice:char;    password:string[30];begin        with platform[target] do        begin                repeat                write('plane ',airplane_no,'is ready to take off,asking for permission(Y/N)?');                readln(choice);                if not(choice in ['Y','y','N','n'])                then write('Wrong input choice,please input choice again(Y/N):');                ClrScr;                until (choice='y') or (choice='Y') or (choice='N') or (choice='n');                if  (choice='N') or (choice='n')                        then airplane_predicted_time_left:=5                        else begin                                repeat                                write('Enter password to execute this order:');                                readln(password);                                ClrScr;                                until password='aed7689';                                writeln('please wait for the processing of taking off.......');                                delay(5000);                                writeln('plane ',airplane_no,'has been taken off');                                readln;                                num:=num-1;                                airplane_no:='empty';                                airplane_predicted_time_left:=999;                             end;        end;end;procedure update_platform_time_left(var num,target,count:integer);begin        target:=0;        if count<=30        then        begin        count:=count+1;        with platform[count] do                begin                        airplane_predicted_time_left:=airplane_predicted_time_left-1;                        if (airplane_predicted_time_left<=0) and (airplane_no<>'')                        then begin                                target:=count;                                if platform[target].airplane_no<>'empty'                                then take_off(num,target);                                if count<31                                then update_platform_time_left(num,target,count);                              end                        else if count<30 then                                        update_platform_time_left(num,target,count);                end;        end;end;procedure update_air_traffic(count:integer;var num,count2:integer);var choice1:char;    target,count1,x,y,n:integer;    password:string[10];begin        n:=1;        target:=0;        count1:=0;        count:=count+1;        if count<=count2                then begin                        with airtraffic[count] do                        begin                                airplane_landing_time_left:=airplane_landing_time_left-1;                                if (airplane_landing_time_left<=0) and (airplane_no<>'')                                then begin                                        repeat                                        write('airplane ',airplane_no,' is asking for the permission of landing(Y/N):');                                        readln(choice1);                                        until (choice1='y') or (choice1='Y') or (choice1='n') or (choice1='N');                                        repeat                                        write('Please input password to continue:');                                        readln(password);                                        ClrScr;                                        until password='aed7689';                                        case choice1 of                                        'y','Y':begin                                                        while target=0 do                                                        begin                                                                count1:=count1+1;                                                                if count1>30                                                                then exit;                                                                if platform[count1].airplane_no='empty'                                                                then target:=count1;                                                        end;                                                        if count1>30                                                        then begin                                                                writeln('No platform remain, please stay on the air');                                                                airplane_landing_time_left:=5;                                                             end                                                        else begin                                                                write('airplane ',airplane_no, ' landing to platform ',target);                                                                readln;                                                                platform[target].airplane_no:=airplane_no;                                                                platform[target].airplane_predicted_time_left:=400;                                                                airplane_no:='';                                                                num:=num+1;                                                                for n:=1 to (count2-1) do                                                                begin                                                                        if airtraffic[n].airplane_no<>''                                                                        then begin                                                                                airtraffic[n].airplane_landing_time_left:=airtraffic[n].airplane_landing_time_left;                                                                             end;                                                                end;                                                             end;                                                        count2:=count2-1;                                                end;                                        'n','N':airplane_landing_time_left:=5                                     end                                     end                                else if count<31                                        then begin                                                update_air_traffic(count,num,count2);                                             end;end;end;end;procedure generate_airplane_no(var count2:integer);var x:array[1..3] of char;    y:array[1..7] of char;    z:string[30];    count,abc:integer;begin        if count2<30        then begin                count2:=count2+1;                for count:=1 to 3 do                x[count]:=' ';                for count:=1 to 7 do                y[count]:=' ';                if count2<21                then begin                        randomize;                        abc:=random(9);                        for count:=1 to 3 do                        x[count]:=chr(random(26)+65);                        for count:=1 to 7 do                        y[count]:=chr(random(10)+48);                        for count:=1 to 2 do                        z:=concat(z,x[count]);                        for count:=1 to 7 do                        z:=concat(z,y[count]);                        with airtraffic[count2] do                        begin                        airplane_no:=z;                        airplane_landing_time_left:=random(20)+1;                        readln;                        end;                      end;                end;end;procedure update_platformfile;var n:integer;begin        rewrite(platformfile);        for n:=1 to 30 do        begin                with platform[n] do                begin                        writeln(platformfile,airplane_no);                        writeln(platformfile,airplane_predicted_time_left);                end;        end;close(platformfile);end;procedure update_airtrafficfile(var count2:integer);var n:integer;begin        rewrite(airtrafficfile);        for n:=1 to count2 do        begin                with airtraffic[n] do                begin                        writeln(airtrafficfile,airplane_no);                        writeln(airtrafficfile,airplane_landing_time_left);                end;        end;close(airtrafficfile);end;procedure output_all;beginwindow(1,50,80,25);readln;clrscr;end;procedure signal_light;beginend;begin        count2:=0;        user_display;        input_airtraffic(count2);        input_platform_condition(num);        while num>=0 do        begin                ClrScr;                GetTime(hour,min,sec,msec);                writeln('Current time: ',hour,':',min,':',sec);                z:=0;                writeln('There are total ',num,' planes in the airport now');                writeln('There are total ',count2,' planes are waiting  for landing');                output_all;                count:=0;                repeat                writeln('Press Enter to continue/Press ESC to exit');                ch:=ReadKey;                ClrScr;                until (ch=#27) or (ch=#13);                case ch of                #13:begin                        if (num=0) and (count2=0)                        then begin                             writeln('No plane information found, please wait until plane land/take off....');                             writeln('press ENTER to continue!!');                             repeat                             ch:=ReadKey;                             until (ch=#13);                             ClrScr;                             end;                        if (num<>0) and (count2<>0)                        then    begin                                generate_airplane_no(count2);                                ClrScr;                                writeln('Waiting for the system instruction.....');                                delay(10000);                                ClrScr;                                update_air_traffic(count,num,count2);                                update_platform_time_left(num,target,count);                                update_platformfile;                                update_airtrafficfile(count2);                                signal_light;                                end;                    end;                #27:exit;                end;        end;end.`
Code: [Select]
`'y','Y':begin                                                        while target=0 do                                                        begin                                                                count1:=count1+1;                                                                if count1>30                                                                then exit;                                                                if platform[count1].airplane_no='empty'                                                                then target:=count1;                                                        end;`Look back procedure update_air_traffic
count1 will not =0 when finding free platform since there are count1:=count1+1
Or i miss sth important?Could you tell to me?
7vinbaby

#### 7vinbaby

• Jr. Member
• Posts: 59
• Hi ! Bless you have a good day.
##### Re: I am the new of pascal .please give me some HELP!
« Reply #25 on: October 10, 2016, 03:58:05 pm »
Code: [Select]
`while (target=0) and (count1>31) do                                                        begin                                                                count1:=count1+1;                                                                if platform[count1].airplane_no='empty'                                                                then target:=count1;                                                        end;`

i have not used this one since as you say 'airplane qwe7634153 landing to platform 0'
7vinbaby

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #26 on: October 10, 2016, 04:18:00 pm »
Look. You can switch back 'n' forth between two faulty versions/implementations but you are only confusing yourself.

The first version that you started out with _always_ go over value 30, and will not allow the plane to land. The other (improved) version (that is also faulty) is _always_ attempting to land on platform 0.

There is something amiss with the logic inside function update_air_traffic.

It seems you want to do several things inside that routine at the same time and you care confusing yourself because the logic is wrong.

if platform[count1].airplane_no='empty' for example, make no sense whatsoever. there is nowhere in your code where you actually assign the word 'empty' to one of the entries., yet you expect the comparison with 'empty' to work. Does that even make sense ?

I would advise to take a step back and think hard on what it is this function is suppose to do exactly and write that down (advisable would be by using a flowchart).

#### Bart

• Hero Member
• Posts: 3897
##### Re: I am the new of pascal .please give me some HELP!
« Reply #27 on: October 10, 2016, 05:08:54 pm »
Code: [Select]
`...        for x:=1 to 30 do          airtraffic[x].airplane_no:='';`
Code: [Select]
`var  airtraffic:array[1..20] of airtraffic_condition;`
Notice the problem?

Bart

#### molly

• Hero Member
• Posts: 2345
##### Re: I am the new of pascal .please give me some HELP!
« Reply #28 on: October 10, 2016, 05:13:12 pm »
OMG! I simply did not noticed that

Bart for president !

And yet another advocate for using Low() to High()

#### Bart

• Hero Member
• Posts: 3897
##### Re: I am the new of pascal .please give me some HELP!
« Reply #29 on: October 10, 2016, 05:58:29 pm »
To me it is a mystery what the application is supposed to do in the first place.
I had to fix several crashes first to get it running.

• Do NOT use hardcode paths for filename
• Do IO checking when reading/writing files
• Do NOT read past the end of a file (you still could)
• If you read from a textfile and you read an integer, read it as string first, then convert to integer (or risk crashing)
• If you want us to test/help, then please, pretty please, leave out the asking for passwords (just comment out that part of the code}
• For the sake of us: use spaces as indentation (2 per level) not Tabs
• When you use ReadKey, clear the keyboard buffer first, otherwise you read keys that user has type whilst waiting and getting frustrated behind his keyboard
• If you do something like delay(), please notify the user that he has to be patient. Because nothing seemed to happen I just hit ^C...
• Use Randomize only once (at program startup)
Some more remarks:

Code: [Select]
`  ...  until (choice='y') or (choice='Y') or (choice='N') or (choice='n');  if  (choice='N') or (choice='n')   `
Compare to

Code: [Select]
`  ...  choice := UpCase(choice);  until (choice in ['Y','N']);  if (choice = 'N')`
After fixing the inital crashes I get:

There are total 30 planes in the airport now
There are total 4 planes are waiting  for landing
then
Press Enter to continue/Press ESC to exit
I press Enter and the application does nothing?
I press Enter again and I get:
airplane       TÿALÜ@ ¤ó@ Ä÷@ P OR534 is asking for the permission of landing(
Y/N):

Notice the strange name?

Here's a hexdump of airtraffic.txt after quitting the program:

Code: [Select]
`C:\Users\Bart\LazarusProjecten\ConsoleProjecten\bugs\Forum\airtraffic>hd airtraffic.txtOffset    Hex                                              ASCII00000010: 66 65 73 32 31 33 35 34 38 37 0D 0A 33 0D 0A 71  fes2135487  3  q00000020: 77 65 37 36 33 34 31 35 33 0D 0A 33 0D 0A 6A 66  we7634153  3  jf00000030: 64 31 33 32 35 37 34 38 0D 0A 37 0D 0A 00 00 00  d1325748  700000040: 00 00 06 00 54 FF 41 01 4C DC 40 00 A4 F3 40 00      T A L @   @00000050: C4 F7 40 00 50 00 4F 52 35 33 34 0D 0A 35 0D 0A    @ P OR534  500000060: 00 00 00 00 00 06 00 54 FF 41 01 4C DC 40 00 A4         T A L @00000070: F3 40 00 C4 F7 40 00 50 00 56 52 37 32 39 0D 0A   @   @ P VR72900000080: 39 0D 0A 00 00 00 00 00 06 00 54 FF 41 01 4C DB  9         T A L00000090: 40 00 A4 F3 40 00 C4 F7 40 00 50 00 4C 52 37 34  @   @   @ P LR74000000A0: 31 0D 0A 34 0D 0A 00 00 00 00 00 06 00 54 FF 41  1  4         T A000000B0: 01 4C DB 40 00 A4 F3 40 00 C4 F7 40 00 50 00 47   L @   @   @ P G000000C0: 46 35 30 36 0D 0A 37 0D 0A 00 00 00 00 00 06 00  F506  7000000D0: 54 FF 41 01 4C DB 40 00 A4 F3 40 00 C4 F7 40 00  T A L @   @   @000000DC: 50 00 46 54 34 35 34 0D 0A 32 30 0D              P FT454  20`
This is because you do not initialize the variable z to an empty string.

Bart
« Last Edit: October 10, 2016, 06:10:42 pm by Bart »