Lazarus

Free Pascal => Beginners => Topic started by: 7vinbaby on October 10, 2016, 09:23:16 am

Title: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 09:23:16 am
I am the new in pascal and i get some problems on my programme.Please read my programme and help to solve it,thanks.
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:integer;
begin
        num:=0;
        for x:=1 to 30 do
        platform[x].airplane_no:='';
        assign(platformfile,'C:/airtrafficcontrol/airport_condition.txt');
        reset(platformfile);
        counter:=0;
        while counter<30 do
        begin
                counter:=counter+1;
                with platform[num] do
                begin
                        readln(platformfile,airplane_no);
                        readln(platformfile,airplane_predicted_time_left);
                        if airplane_no<>'$$$'
                        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(20000);
                                writeln('plane ',airplane_no,'has been taken off');
                                readln;
                                num:=num-1;
                                airplane_no:='$$$';
                                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<>'$$$'
                                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='$$$'
                                                                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;
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');
                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);
                                end;
                    end;
                #27:exit;


                end;
        end;
end.

here is my code and after executing many procedures , i try to save my record and update my textfile.However i stuck on my saving of update_platformfile. the data saved is out of my expectation.

what i want is:
Code: [Select]
$$$
999
ays5598745
4
afj7895542
11
asw7865456
17
afu7641313
22
hfi7646411
29
dai5641321
40
ash7646132
49
ash4564653
58
kjs8463133
63
kja7645413
69
ahf4613133
74
sad8976463
81
qwr2131852
88
hdf1341679
93
djd7641232
98
qwr7841321
101
dsa7643125
110
fdd4632154
116
thr5412312
121
vxj7643135
125
jhd6734535
131
rjn8764354
138
fds8645643
141
her7634351
150
hbg5643515
153
ery4123156
160
few3131548
166
$$$
999
$$$
999

however after updated i got
Code: [Select]
afj7895542
4
asw7865456
10
afu7641313
15
hfi7646411
22
dai5641321
33
ash7646132
42
ash4564653
51
kjs8463133
56
kja7645413
62
ahf4613133
67
sad8976463
74
qwr2131852
81
hdf1341679
86
djd7641232
91
qwr7841321
94
dsa7643125
103
fdd4632154
109
thr5412312
114
vxj7643135
118
jhd6734535
124
rjn8764354
131
fds8645643
134
her7634351
143
hbg5643515
146
ery4123156
153
few3131548
159
HK6206933
393

-7

-7

-7

i am sorry that it costs a lot of time of teaching a novice but
Could you help to figure out what i get wrong on my programme or on my logic,please?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 10:40:38 am
Hi 7vinbaby,

Unfortunately the program crashes for me on startup when attempting to read the files.

Reason for that is when you do not have the files present you still attempt to open them.

Would you like to address that first ? And are you able to manage that on your own ?

As a hint, please have a look at the reset command (http://www.freepascal.org/docs-html/rtl/system/reset.html)

Do you notice how the {$I-} and {$I+} switches being used in the example shown at the bottom ?

That is so that you are able to 'catch' the IOResult variable in order to be able check if the file opened correctly or not.


PS: And please do me/us/yourself a favour and declare a global constant with the password for testing purposes so that one is able to change that with one single change of that password. It is highly annoying to get prompted for the password over and over again. If you declare it globally one could change it into nothing and simply press enter.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 10:52:45 am

Unfortunately the program crashes for me on startup when attempting to read the files.

actually i think the program crashes for you is because the file name as i put the textfile in C drive(if i do not misunderstand your word).
Also i think it is not the problems of opening the file but the problems of saving record.
Because in the textfile,if there is empty ,i use $$$ to represent .
But after saving i cannot find any $$$ but becomes -7.

Did i make anything wrong or did i misunderstand sth?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 10:56:26 am
actually i think the program crashes for you is because the file name as i put the textfile in C drive(if i do not misunderstand your word).
I was smart enough to change that. Still i do not have those files and your code is not taking that situation into account.

Please try yourself by renaming your existing files that your porgram uses and witness the crash yourself.

The first crash occurs inside routine input_airtraffic() when you do the reset.

Hence why i pointed towards the documentation on that subject  :)

Quote
Did i make anything wrong or did i misunderstand sth?
Yes  :D. your code  wrongfully assumes the files always exist while in my case they don't.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 11:05:36 am
actually i think the program crashes for you is because the file name as i put the textfile in C drive(if i do not misunderstand your word).
I was smart enough to change that. Still i do not have those files and your code is not taking that situation into account.

Please try yourself by renaming your existing files that your porgram uses and witness the crash yourself.

The first crash occurs inside routine input_airtraffic() when you do the reset.

Hence why i pointed towards the documentation on that subject  :)

Quote
Did i make anything wrong or did i misunderstand sth?
Yes  :D. your code  wrongfully assumes the files always exist while in my case they don't.

i get loss on what you pointed out.

Btw the update_platformfile and update_airtrafficfile should have something wrong on saving the record into the textfile
        Because in the textfile,if it is empty record ,i use $$$ to represent .
        But after saving i cannot find any $$$ but becomes -7 in the textfile.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 11:15:35 am
Life is pretty simple  :D

You wrote:
Quote
what i want is:
blah

however after updated i got
blah

It is not possible for someone else to verify this situation as the files that your program uses are not part of your post.

So, whenever someone else starts your program they will run your program for the very first time.

For that situation there are no files present yet, and so your program crashes.

It is impossible for someone else (besides yourself) to generate the result as you have shown in your post. I'm not in the mood to invent files and their data for your program as i have not the faintest idea what i must put inside such files.

Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 11:23:35 am
Life is pretty simple  :D

You wrote:
Quote
what i want is:
blah

however after updated i got
blah

It is not possible for someone else to verify this situation as the files that your program uses are not part of your post.

So, whenever someone else starts your program they will run your program for the very first time.

For that situation there are no files present yet, and so your program crashes.

It is impossible for someone else (besides yourself) to generate the result as you have shown in your post. I'm not in the mood to invent files and their data for your program as i have not the faintest idea what i must put inside such files.

okay,so what i should do for this to correct this?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 11:46:16 am
okay,so what i should do for this to correct this?
If you want others to be able to produce the exact same output as you do, then provide us with the missing file(s).

In case you can't share the files you are currently using, then make/provide us some (unimportant) version of the file for us to work with and that you can use yourself for comparison. In that case make sure the error that you experience is still present (it can depend on the actual data that is stored inside your file).

Better would be to let your program generate the files automatically but, i can imagine that you removed those parts from your initial post in order to keep your example as small as possible.  :)

Either way, your code does need to take care of the situation were it is unable to reset the file and act accordingly (for example printing a human readable error instead of runtime error 2 (http://www.freepascal.org/docs-html/user/userap4.html))

The example code for the fileexists function that i pointed to earlier (link (http://www.freepascal.org/docs-html/rtl/system/reset.html)) would be a good start for you to be able to determine if the required file actually exists or not. As of yet i have no clue what your program should do in case it is unable to find the file other then telling the user that the program was unable to find the file.

Perhaps you have an idea about that yourself ?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 11:56:48 am
okay,so what i should do for this to correct this?
If you want others to be able to produce the exact same output as you do, then provide us with the missing file(s).

In case you can't share the files you are currently using, then make/provide us some (unimportant) version of the file for us to work with and that you can use yourself for comparison. In that case make sure the error that you experience is still present (it can depend on the actual data that is stored inside your file).

Better would be to let your program generate the files automatically but, i can imagine that you removed those parts from your initial post in order to keep your example as small as possible.  :)

Either way, your code does need to take care of the situation were it is unable to reset the file and act accordingly (for example printing a human readable error instead of runtime error 2 (http://www.freepascal.org/docs-html/user/userap4.html))

The example code for the fileexists function that i pointed to earlier (link (http://www.freepascal.org/docs-html/rtl/system/reset.html)) would be a good start for you to be able to determine if the required file actually exists or not. As of yet i have no clue what your program should do in case it is unable to find the file other then telling the user that the program was unable to find the file.

Perhaps you have an idea about that yourself ?

i see what you mean.
But the problems are that i cannot have a correct data in my textfile even the files exist.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 12:02:59 pm
i see what you mean.
But the problems are that i cannot have a correct data in my textfile even the files exist.
Then we have a problem  :D

If i create those files and leave them empty, all i get is:
Quote
Current time: 10:57:13
There are total 30 planes in the airport now
There are total 0 planes are waiting  for landing
Press Enter to continue/Press ESC to exit

So, i am unable to reproduce the error that you got  :'(

What must i do do reproduce the exact error that you got ?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 12:06:40 pm
i see what you mean.
But the problems are that i cannot have a correct data in my textfile even the files exist.
Then we have a problem  :D

If i create those files and leave them empty, all i get is:
Quote
Current time: 10:57:13
There are total 30 planes in the airport now
There are total 0 planes are waiting  for landing
Press Enter to continue/Press ESC to exit

So, i am unable to reproduce the error that you got  :'(

What must i do do reproduce the exact error that you got ?

actually there are also a textfile for airtrafficfile.
Code: [Select]
.....
 assign(airtrafficfile,'C:/airtrafficcontrol/airtraffic.txt');
........
the textfile is like that:
Code: [Select]
fes2135487
3
qwe7634153
7
jfd1325748
11

sorry for taking you long time!
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 12:09:52 pm
Also the problems occur after i add procedure update_platformfile & update_airtrafficfile since i want to save the record even the users leave the programme but not restart.
Before this ,the programme is all right ( i think).
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 12:30:09 pm
Ok, thank you. That 3 flight entries example helped start up the program  :)

Without actually looking at the code, i think i spotted some things that go wrong:
1) when i press escape to stop the process the file airport_condition.txt is filled with a crlf + number. That does not seem to make any sense to me.
2) no matter what i choose when a plane asks if it is allowed to land: it always adds up the number of planes that are waiting for landing (i would expect that number to decrease when i actually give permission for the plane to land).
3) after the message Press enter to continue/Press ESC key, and when pressing enter the screen is cleared and a cursor stands at top left corner without any message. It took me a while to figure out i had to press the return key (i was expecting a delay of about 20 seconds as i peeked in your code  :D )

This will require a bit more time to debug but, at the same time i am not sure what is the intention of the program.

In case my writing tells you that something is wrong, then please state how the program is suppose to behave. Right now my experience is that the program behaves  how the code was written. If that is correct or not is another story  :)
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 12:47:12 pm
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:integer;
begin
        num:=0;
        for x:=1 to 30 do
        platform[x].airplane_no:='';
        assign(platformfile,'C:/airtrafficcontrol/airport_condition.txt');
        reset(platformfile);
        counter:=0;
        while counter<30 do
        begin
                counter:=counter+1;
                with platform[num] do
                begin
                        readln(platformfile,airplane_no);
                        readln(platformfile,airplane_predicted_time_left);
                        if airplane_no<>'$$$'
                        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:='$$$';
                                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<>'$$$'
                                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='$$$'
                                                                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;
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');
                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);
                                for q:=1 to 30 do
                                begin
                                with platform[q] do
                                begin
                                writeln(airplane_no);
                                writeln(airplane_predicted_time_left);
                                end;
                                readln;
                                end;
                                end;
                    end;
                #27:exit;


                end;
        end;
end.

you can try the above code which output the stored data in record of platformfile
i add some codes which output the record of platform
and you will find the record from $$$ to -1 &-2&-3 which i expect if the record is empty ,it is $$$.
That's all the reasons that go wrong including no plane to take off.
Also the no of plane to land will not decrease since it is the waiting list of landing plane and generate automatically.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 12:58:33 pm
My programme procedure is :(modify the programme timing)

read the data in textfile .
wait for 60s (5s)
find any plane on air or in airport 's time is 0 ,means it is ready to take off or landing.
then, if time is 0
         take off/landing procedure. Then goto save record and loop repeatly
otherwise goto save record and loop repeatly
finally save record in textfile ..
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby on October 10, 2016, 02:03:23 pm
.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly 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
  15.       readln(platformfile,airplane_no);
  16.       readln(platformfile,airplane_predicted_time_left);
  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.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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
  15.       readln(platformfile,airplane_no);
  16.       readln(platformfile,airplane_predicted_time_left);
  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)?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly 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');
  7.                 ReadLn;
  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  :D

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 ?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly 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.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly 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?
Talking about array index here:

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.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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;
begin
window(1,50,80,25);
readln;
clrscr;
end;
procedure signal_light;
begin
end;
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?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: 7vinbaby 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'
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly 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).
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart 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
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 05:13:12 pm
OMG! I simply did not noticed that  :-[

Bart for president ! :D

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

Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart 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.

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.txt
Offset    Hex                                              ASCII
00000010: 66 65 73 32 31 33 35 34 38 37 0D 0A 33 0D 0A 71  fes2135487  3  q
00000020: 77 65 37 36 33 34 31 35 33 0D 0A 33 0D 0A 6A 66  we7634153  3  jf
00000030: 64 31 33 32 35 37 34 38 0D 0A 37 0D 0A 00 00 00  d1325748  7
00000040: 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  5
00000060: 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 VR729
00000080: 39 0D 0A 00 00 00 00 00 06 00 54 FF 41 01 4C DB  9         T A L
00000090: 40 00 A4 F3 40 00 C4 F7 40 00 50 00 4C 52 37 34  @   @   @ P LR74
000000A0: 31 0D 0A 34 0D 0A 00 00 00 00 00 06 00 54 FF 41  1  4         T A
000000B0: 01 4C DB 40 00 A4 F3 40 00 C4 F7 40 00 50 00 47   L @   @   @ P G
000000C0: 46 35 30 36 0D 0A 37 0D 0A 00 00 00 00 00 06 00  F506  7
000000D0: 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
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 10, 2016, 06:19:42 pm
And yet another crash in update_airtrafficfile, because count2 = 21....

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 10, 2016, 07:06:22 pm
About generation airplane number:
The Arrays are not necessary.
This will do nicely.

Code: [Select]
  z := '';
  for count :=  1 to 2 do z := z + chr(random(26)+65);
  for count := 1 to 7 do z := z + chr(random(10)+48);

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 07:11:29 pm
@Bart:
afaik the idea is that the program is reading data from two files (i have adjusted my version to create them if not present).

One file's data represent an airport with plane numbers and their 'time-out'. The other file is 'the air' where planes can fly around.
In the latter file also plane number and 'time-out' value.

The program stores these data into arrays and then randomly (and also based on time-out) decides what the planes are going to do
- land on airpoirt
- departure from airport

At the same time, new planes are 'flying' in randomly.

The prorgram asks for permission if a plane is allowed to land or not. In case not it sets an new timeout value and processes the queue accordingly to time out values.

The best i can do for the moment as i'm a little busy atm.
Code: Pascal  [Select][+][-]
  1. Program air_traffic_control;
  2.  
  3. {$RANGECHECKS ON}{$OVERFLOWCHECKS ON}{$HINTS ON}
  4.  
  5. Uses
  6.   Dos, Crt, SysUtils, Classes;
  7.  
  8. const
  9.   DefaultPW              = '';
  10.   SystemInstructionDelay = 5000;
  11.   airtrafficfilename     = 'airtraffic.txt';
  12.   platformfilename       = 'airport_condition.txt';
  13.  
  14.  
  15. Type
  16.   Tairport_platform =
  17.   Record
  18.     airplane_no                  : string[30];
  19.     airplane_predicted_time_left : integer;
  20.   End;
  21.  
  22.   Tairtraffic_condition =
  23.   Record
  24.     airplane_no                  : string[30];
  25.     airplane_landing_time_left   : integer;
  26.   End;
  27.  
  28.  
  29. Var
  30.   hour                 : word = 0;
  31.   min                  : word = 0;
  32.   sec                  : word = 0;
  33.   msec                 : word = 0;
  34.   NumberOfLandedPlanes : integer = 0;
  35.   target               : integer = 0;
  36.   count,
  37.   count2,
  38.   z                    : integer;
  39.   platformfile,
  40.   airtrafficfile       : text;
  41.   platform             : array[1..30] of Tairport_platform;
  42.   airtraffic           : array[1..20] of Tairtraffic_condition;
  43.   ch                   : char;
  44.  
  45.  
  46. Procedure user_display;
  47. type
  48.   TDayType = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);
  49. var
  50.   YY        : Word = 0;
  51.   MM        : word = 0;
  52.   DD        : word = 0;
  53.   DayOfWeek : Word = 0;
  54.   hour      : Word = 0;
  55.   min       : Word = 0;
  56.   sec       : Word = 0;
  57.   msec      : Word = 0;
  58.   today     : TDayType;
  59.   password  : string[20];
  60. begin
  61.   textbackground(black);
  62.   ClrScr;
  63.   Getdate(YY,MM,DD,DayOfWeek);
  64.   GetTime(hour,min,sec,msec);
  65.   Today := TDayType(DayOfWeek);
  66.   textcolor(yellow);
  67.   writeln('Welcome to execute air traffic control');
  68.  
  69.   repeat
  70.     textcolor(yellow);
  71.     write('Password:');
  72.     readln(password);
  73.     if password<>DefaultPW
  74.     then writeln('Wrong password,enter again');
  75.   until password=DefaultPW;
  76.  
  77.   ClrScr;
  78.   writeln('Day of Today:',DD,'/',MM,'/',YY,',',Today);
  79.   writeln('Current time: ',hour,':',min,':',sec);
  80.   writeln('Now start the system of air traffic control management');
  81.   repeat
  82.     textcolor(yellow);
  83.     write('Enter password again to continue:');
  84.     readln(password);
  85.     if password<>DefaultPW
  86.     then writeln('Wrong password,enter again');
  87.   until password=DefaultPW;
  88.   ClrScr;
  89. end;
  90.  
  91.  
  92. Procedure input_airtraffic(Var count2:integer);
  93. var n: integer;
  94. Begin
  95.   for n:= Low(airtraffic) to High(airtraffic)
  96.     do airtraffic[n].airplane_no := '';
  97.  
  98.   count2 := 0;
  99.   assign(airtrafficfile, airtrafficfilename);
  100.   reset(airtrafficfile);
  101.  
  102.   while not eof(airtrafficfile) do
  103.   begin
  104.     count2 := count2 + 1;
  105.     with airtraffic[count2] do
  106.     begin
  107.       readln(airtrafficfile,airplane_no);
  108.       readln(airtrafficfile,airplane_landing_time_left);
  109.     end;
  110.   end;
  111.  
  112.   close(airtrafficfile);
  113. End;
  114.  
  115.  
  116. procedure input_platform_condition(var num:integer);
  117. var
  118.   idx, n                       : integer;
  119.   airplane_no                  : String[30] = '';
  120.   airplane_predicted_time_left : integer    = 0;
  121. begin
  122.   Writeln('dumping airport plane stats');
  123.   num :=0;
  124.   for n := Low(platform) to High(platform)
  125.     do platform[n].airplane_no := '';
  126.  
  127.   assign(platformfile, platformfilename);
  128.   reset(platformfile);
  129.  
  130.   idx := 0;
  131.   while not eof(platformfile) do
  132.   begin
  133.     if (idx < High(platform)) then inc(idx) else break;
  134.  
  135.     with platform[idx] do
  136.     begin
  137.       readln(platformfile,airplane_no);
  138.       readln(platformfile,airplane_predicted_time_left);
  139.  
  140.       if (airplane_no <> '') then
  141.       begin
  142.         num := num + 1;
  143.         Writeln('[ok] Plane = ', airplane_no, ' predicted_time_left = ', airplane_predicted_time_left);
  144.       end
  145.       else
  146.         Writeln('[xx] Plane = ', airplane_no, ' predicted_time_left = ', airplane_predicted_time_left);
  147.     end;
  148.   end;
  149.   close(platformfile);
  150.  
  151.   WriteLn('Press enter to continue'); ReadLn;
  152. end;
  153.  
  154.  
  155. Procedure take_off(Var num,target:integer);
  156. Var
  157.   choice: char;
  158.   password: string[30];
  159. Begin
  160.   With platform[target] Do
  161.     Begin
  162.       Repeat
  163.         write('plane ',airplane_no,'is ready to take off,asking for permission(Y/N)?');
  164.         readln(choice);
  165.         If Not(choice In ['Y','y','N','n'])
  166.           Then write('Wrong input choice,please input choice again(Y/N):');
  167.         ClrScr;
  168.       Until (choice='y') Or (choice='Y') Or (choice='N') Or (choice='n');
  169.       If  (choice='N') Or (choice='n')
  170.         Then airplane_predicted_time_left := 5
  171.       Else
  172.         Begin
  173.           Repeat
  174.             write('Enter password to execute this order:');
  175.             readln(password);
  176.             ClrScr;
  177.           Until password=DefaultPW;
  178.           writeln('please wait for the processing of taking off.......');
  179.           delay(20000);
  180.           writeln('plane ',airplane_no,'has been taken off');
  181.           readln;
  182.           num := num-1;
  183.           airplane_no := '$$$';
  184.           airplane_predicted_time_left := 999;
  185.  
  186.         End;
  187.     End;
  188. End;
  189.  
  190.  
  191. procedure update_platform_time_left(Var num,target,count:integer);
  192. begin
  193.   target := 0;
  194.   if count <= High(platform) then
  195.   begin
  196.     count := count+1;
  197.     with platform[count] do
  198.     begin
  199.       airplane_predicted_time_left := airplane_predicted_time_left-1;
  200.       if (airplane_predicted_time_left<=0) and (airplane_no <> '') then
  201.       begin
  202.         target := count;
  203.         if (platform[target].airplane_no <> '$$$') then take_off(num,target);
  204.         if (count <= High(platform))
  205.         then update_platform_time_left(num,target,count);
  206.       end
  207.       else if count < High(platform)
  208.            then update_platform_time_left(num, target, count);
  209.     end;
  210.   end;
  211. end;
  212.  
  213.  
  214. Procedure update_air_traffic(count:integer;Var num,count2:integer);
  215. Var
  216.   choice1: char;
  217.   target,count1, n: integer;
  218.   password: string[10];
  219. Begin
  220.   n := 1;
  221.   target := 0;
  222.   count1 := 0;
  223.   count := count+1;
  224.  
  225.   If count<=count2 then
  226.   Begin
  227.     With airtraffic[count] Do
  228.     Begin
  229.       airplane_landing_time_left := airplane_landing_time_left-1;
  230.       If (airplane_landing_time_left<=0) And (airplane_no<>'') Then
  231.       Begin
  232.         Repeat
  233.           write('airplane ',airplane_no,' is asking for the permission of landing(Y/N):');
  234.           readln(choice1);
  235.         Until (choice1='y') Or (choice1='Y') Or (choice1='n') Or (choice1='N');
  236.         Repeat
  237.           write('Please input password to continue:');
  238.           readln(password);
  239.           ClrScr;
  240.         Until password=DefaultPW;
  241.  
  242.         Case choice1 Of
  243.           'y','Y':
  244.           Begin
  245.             Writeln('chosen to allow landing, searching for a free platform');
  246.  
  247.             Target := -1;
  248.             for count1 := 1 to Length(platform) do
  249.             begin
  250.               if platform[count1].airplane_no = '' then
  251.               begin
  252.                 target := count1;
  253.                 break;
  254.               end;
  255.             end;
  256.  
  257.             WriteLn('The target platform is ', target);
  258.             WriteLn('The value of count1 is ', count1);
  259.  
  260.             if target < 1 then
  261.             Begin
  262.               writeln('No platform remain, please stay on the air');
  263.               airplane_landing_time_left := 5;
  264.             end
  265.             else
  266.             begin
  267.               write('airplane ',airplane_no, ' landing to platform ',target);
  268.               readln;
  269.               platform[target].airplane_no := airplane_no;
  270.               platform[target].airplane_predicted_time_left := 400;
  271.               airplane_no := '';
  272.               num := num+1;
  273.               For n:=1 To (count2-1) Do
  274.               Begin
  275.                 If airtraffic[n].airplane_no<>'' Then
  276.                 Begin
  277. {???}             airtraffic[n].airplane_landing_time_left := airtraffic[n].airplane_landing_time_left;
  278.                 End;
  279.               End;
  280.             End;
  281.             count2 := count2-1;
  282.           End;
  283.           'n','N': airplane_landing_time_left := 5
  284.         End  { case choice }
  285.       End
  286.  
  287.       Else
  288.  
  289. //      if count <= High(airtraffic) then
  290.       if count < High(airtraffic) then
  291.       begin
  292.         update_air_traffic(count,num,count2);
  293.       end;
  294.     end;
  295.   end;
  296. end;
  297.  
  298.  
  299. Procedure generate_airplane_no(Var count2:integer);
  300. Var
  301.   x: array[1..3] Of char;
  302.   y: array[1..7] Of char;
  303.   z: string[30] = '';
  304.   count,abc: integer;
  305. Begin
  306. // ????
  307.   If count2< 30 Then
  308.   Begin
  309.     count2 := count2+1;
  310.     for count:= Low(x) to High(x)
  311.       do x[count] := ' ';
  312.     for count:= Low(y) to High(y)
  313.       do y[count] := ' ';
  314.  
  315.     if count2 <= High(airtraffic) then
  316.     begin
  317.       randomize;
  318.       abc := random(9);
  319.       for count:= Low(x) to High(x)
  320.         do x[count] := chr(random(26)+65);
  321.       for count:= Low(y) to High(y)
  322.         do y[count] := chr(random(10)+48);
  323.       for count := 1 To 2
  324.         do z := concat(z, x[count]);
  325.       for count := low(y) to High(y)
  326.         do z := concat(z, y[count]);
  327.  
  328.       with airtraffic[count2] do
  329.       begin
  330.         airplane_no := z;
  331.         airplane_landing_time_left := random(20)+ 1;  // ??? High(airtraffic) ?
  332.         readln;
  333.       end;
  334.     end;
  335.   end;
  336. end;
  337.  
  338.  
  339. Procedure update_platformfile;
  340. var
  341.   n: integer;
  342. begin
  343.   rewrite(platformfile);
  344.   for n := Low(PlatForm) to High(Platform) do
  345.   begin
  346.     with platform[n] do
  347.     begin
  348.       writeln(platformfile,airplane_no);
  349.       writeln(platformfile,airplane_predicted_time_left);
  350.     end;
  351.   end;
  352.   close(platformfile);
  353. end;
  354.  
  355.  
  356. Procedure update_airtrafficfile(Var count2:integer);
  357. Var n: integer;
  358. Begin
  359.   rewrite(airtrafficfile);
  360.   For n:=1 To count2 Do
  361.   Begin
  362.     With airtraffic[n] Do
  363.     Begin
  364.       writeln(airtrafficfile,airplane_no);
  365.       writeln(airtrafficfile,airplane_landing_time_left);
  366.     End;
  367.   End;
  368.   close(airtrafficfile);
  369. End;
  370.  
  371.  
  372. Begin
  373.   if not fileexists(airtrafficfilename) then
  374.   begin
  375.     with TStringList.Create do
  376.     begin
  377.       Append('fes2135487');
  378.       Append('3');
  379.       Append('qwe7634153');
  380.       Append('7');
  381.       Append('jfd1325748');
  382.       Append('11');
  383.       SaveToFile(airtrafficfilename);
  384.       Free;
  385.     end;
  386.   end;
  387.  
  388.   if not fileexists(platformfilename) then
  389.   begin
  390.     with TStringList.Create do
  391.     begin
  392.       SaveToFile(platformfilename);
  393.       Free;
  394.     end;
  395.   end;
  396.  
  397.   count2 := 0;
  398.   user_display;
  399.   input_airtraffic(count2);
  400.   input_platform_condition(NumberOfLandedPlanes);
  401.  
  402.   While NumberOfLandedPlanes>=0 Do
  403.   Begin
  404.     ClrScr;
  405.     GetTime(hour,min,sec,msec);
  406.     writeln('Current time: ',hour,':',min,':',sec);
  407.     z := 0;
  408.     writeln('There are total ',NumberOfLandedPlanes,' planes in the airport now');
  409.     writeln('There are total ',count2,' planes are waiting  for landing');
  410.     count := 0;
  411.     Repeat
  412.       writeln('Press Enter to continue/Press ESC to exit');
  413.       ch := ReadKey;
  414.       ClrScr;
  415.     Until (ch=#27) Or (ch=#13);
  416.  
  417.     Case ch Of
  418.       #13:
  419.       Begin
  420.         if ( (NumberOfLandedPlanes=0) and (count2=0) ) then
  421.         Begin
  422.           writeln('No plane information found, please wait until plane land/take off....');
  423.           writeln('press ENTER to continue!!');
  424.           Repeat
  425.             ch := ReadKey;
  426.           Until (ch=#13);
  427.           ClrScr;
  428. //      End;
  429. //      If (num<>0) And (count2<>0) Then
  430.         end
  431.         else
  432.         Begin
  433.           generate_airplane_no(count2);
  434.           ClrScr;
  435.           writeln('Waiting for the system instruction.....');
  436.           delay(SystemInstructionDelay);
  437.           ClrScr;
  438.           update_air_traffic(count, NumberOfLandedPlanes, count2);
  439.           update_platform_time_left(NumberOfLandedPlanes, target, count);
  440.           update_platformfile;
  441.           update_airtrafficfile(count2);
  442. {
  443.           for q:=1 to 30 do
  444.           begin
  445.             with platform[q] do
  446.             begin
  447.               writeln(airplane_no);
  448.               writeln(airplane_predicted_time_left);
  449.             end;
  450.             readln;
  451.           end;
  452. }
  453.         End;
  454.       End;
  455.       #27: exit;
  456.     End;  { case ch }
  457.   End;
  458. End.
  459.  

Still the range-check errors as i am not sure on what information some of the used values are based on.

Note that function update_air_traffic is my own, as well as added as much high() low() as possible (i might have gotten one or two wrong there as well).
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 10, 2016, 07:42:10 pm
I semi-automated the program to never ask passwords, always accept landing planes, never accept taking off.

Then you get the runtime error with count2=21.

The file reading is accident prone.
Checking for eof() and the doing 2 readln's is plain wrong!
Also I screwed up the airtraffic.txt file, so that the order of lines was wrong. That did not go well.

Also I don't like a procedure that alters both a global variable and a var parameter (which happens to be global as well). Yuck.

For debugging purposes the program should be developed without the use of crt unit.
I cannot read anything back on my console when things go wrong.

Some thoughts:
Use Ini-files for the 2 textfiles, or use a binary file (file of record).
Alternatively use TStringList to read/write the textfiles.
Give variables a logical name.
Use the least possible number of global variables.
If a procedure alters a variable, then put it in the parameterlist.
Use empty lines between procedures.
As you did: define constants for the array type definitions (array[1..MaxNoPlanes] of ...)
Have all checks on (rangecheck, iocheck etc)
(I develop using Lazarus and all new projects have a debug and release builmode)
When using old-style reading/writing with files check IOResult after each read/write/reset/rewrite/close.
Re-use code (asking for password comes to mind).

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 10, 2016, 09:22:04 pm
Ah, i think i found the last bugger :D

Code: Pascal  [Select][+][-]
  1. procedure update_platform_time_left(Var aNrLandedPlanes,target,count:integer);
  2. begin
  3.   target := 0;
  4.   if count <= High(platform) then
  5.   begin
  6.     count := count+1;
  7.     with platform[count] do
  8.     begin
  9.       airplane_predicted_time_left := airplane_predicted_time_left - 1;
  10.       if (airplane_predicted_time_left<=0) and (airplane_no <> '') then
  11.       begin
  12.         target := count;
  13.         if (platform[target].airplane_no <> '$$$') then take_off(aNrLandedPlanes, target);
  14.         if (count <= High(platform))
  15.         then update_platform_time_left(aNrLandedPlanes, target, count);
  16.       end
  17.       else if count < High(platform)
  18.            then update_platform_time_left(aNrLandedPlanes, target, count);
  19.     end;
  20.   end;
  21. end;
  22.  

the highlighted line should (obviously) read: if count < High(platform) then.

Things seem to work with my version so far.

Still the strange/wierd comparison with $$$ that isn't set in any of the .airplane_no in the code. TS would need to confirm how that is to be dealt with (just as the comparisons against .airplane_no being 'empty').
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 11, 2016, 06:05:26 pm
@7vinbaby:
The logic part that is giving you issues is (afaik) the following:

You have two static arrays (one for the planes at the airport and one for the planes that are in the air).

For the platform array you got things pretty much covered, in that you always write (and restore) the complete array to and from file.

But, the airtraffic array is left behind somewhere along the way. The array is static while you add items dynamically to the array (you use the count2 variable to keep track of how many items are 'valid' in that array).

In principle that is not a problem but you have a logic error in there in the fact that whenever you 'transfer' a plane from "flying in the air" to "being grounded at an airport" that you do not update the static airtraffic array accordingly in order to reflect the new situation for the plane that was just 'removed' from that array.

Instead you simply decrease the number of planes in the air, and do not update the contents of the array.

imho you have mixed two things there. either you
a) use the airtraffic in a similar way as your platform array in that it has a fixed number of positions and every position reflect the current status. That way you can check for that status each time you require information from that array
or
b) simulate the array being dynamic (as you are doing right now) by keeping track of the number of "valid" items in there. But when you do so, also update the items in the array according to the action being taken (e.g. adding a plane to that array 'extends' things while removing an item from that array means you need to update the items accordingly, e.g. 'delete' the removed item form the array.

In my working version i have not added a solution for this yet, which is why i am able to see where things go wrong.

My advise would be to also make the airtraffic array 'static' in that all entries in that array _must_ contain valid information that can be accessed and make sense _all_ the time. That way you can simply designate an itemslot to be "empty", or containing a valid flight number. A real empty string would then become a non valid value for the field airplane_no.

But, i can not decide this for you. You would have to make sure that is what you actually want. I can imagine that you want the airtraffic.txt file as small as possible but as said, you then would have to make sure that the airtraffic array does not contain any 'invalid' entries.

A better advise would probably be to tell to use OOP (or at least advanced records so that you can add/use record functions), as doing so would make things much more clear and all the logic can then be kept inside the object/record itself. Make a good implemented object/record/class that follows your given set of rules and you would never have to take a look at it again.


Edit: version based on array 'moving' items
Code: Pascal  [Select][+][-]
  1. program air_traffic_control;
  2.  
  3. {$RANGECHECKS ON}{$OVERFLOWCHECKS ON}{$HINTS ON}
  4.  
  5. {.$DEFINE SKIP_PASSWORDPROMPT}
  6. {.$DEFINE SKIP_CHOICEPOSITIVE}
  7. {.$DEFINE SKIP_WAITS}
  8.  
  9. uses
  10.   Dos, Crt;
  11.  
  12. const
  13.   DefaultPW              = '';
  14.   SystemInstructionDelay = 1000;
  15.   TakeOffDelay           = 20000;
  16.   airtrafficfilename     = 'airtraffic.txt';
  17.   platformfilename       = 'airport_condition.txt';
  18.  
  19.  
  20. type
  21.   Tairport_platform =
  22.   record
  23.     airplane_no                  : string[30];
  24.     airplane_predicted_time_left : integer;
  25.   end;
  26.  
  27.   Tairtraffic_condition =
  28.   record
  29.     airplane_no                  : string[30];
  30.     airplane_landing_time_left   : integer;
  31.   end;
  32.  
  33.  
  34. var
  35.   hour                 : word = 0;
  36.   min                  : word = 0;
  37.   sec                  : word = 0;
  38.   msec                 : word = 0;
  39.   NrLandedPlanes       : integer = 0;   // previously named: Num
  40.   NrAirbornePlanes     : integer = 0;   // previously named: Count2
  41.   target               : integer = 0;
  42.   count                : integer;
  43.   z                    : integer;
  44.   platformfile,
  45.   airtrafficfile       : text;
  46.   platform             : array[1..30] of Tairport_platform;
  47.   airtraffic           : array[1..20] of Tairtraffic_condition;
  48.   ch                   : char;
  49.  
  50.  
  51. // Displays PromptMsg and awaits input from keyboard to match global PassWord
  52. // definition. If the PassWord does not match the HintMsg is displayed until
  53. // PassWord matches. In case HintMsg is empty then the screen is cleared and
  54. // the PromptMsg is repeated.
  55. // The routine loops until userinput matches password
  56. procedure PromptForPassWord(aPromptMsg: string = 'Password:'; aHintMsg: string = '');
  57. var
  58.   password  : string[20];
  59. begin
  60.   {$IFNDEF SKIP_PASSWORDPROMPT}
  61.   repeat
  62.     write(aPromptMsg);
  63.     readln(password);
  64.  
  65.     if (aHintMsg <> '') then
  66.     begin
  67.       if (password <> DefaultPW) then WriteLn(aHintMsg);
  68.     end
  69.     else ClrScr;
  70.   until password=DefaultPW;
  71.   {$ENDIF}
  72. end;
  73.  
  74.  
  75. // Copy from http://www.freepascal.org/docs-html/rtl/system/reset.html
  76. // Note that the original code had an error in that if the file does not
  77. // exist the example is still atempting to close the file -> error.
  78. // This code has that issue fixed.
  79. // Check if a given filename/path exists and returns a true/false answer
  80. function FileExists (aFileName: string) : boolean;
  81. var
  82.   f : File;
  83. begin
  84.   if (aFileName <> '') then
  85.   begin
  86.     {$i-}
  87.     Assign (f, aFileName);
  88.     Reset (f);
  89.     {$i+}
  90.     if (IOResult = 0) then
  91.     begin
  92.       Close (f);
  93.       Exit(true);
  94.     end;
  95.   end;
  96.   FileExists := false;
  97. end;
  98.  
  99.  
  100. // Creates a new texfile from given Filename and returns filepointer
  101. // in FileVar. In case things go wrong the function halts the program (panic).
  102. procedure CreateNewTextFile(aFileName: string; var aFileVar: Text);
  103. var
  104.   LastError : Word = 0;
  105. begin
  106.   if (aFileName <> '') then
  107.   begin
  108.     {$i-}
  109.     Assign (aFileVar, aFileName);
  110.     Rewrite (aFileVar);
  111.     {$i+}
  112.     LastError := IoResult;
  113.  
  114.     if (LastError = 0) then Close(aFileVar) else
  115.     begin
  116.       WriteLn('Fatal error ! Unable to create file ', aFileName, ' (IOResult = ', LastError, ')');
  117.       Halt(1);
  118.     end;
  119.   end
  120.   else
  121.   begin
  122.     WriteLn('Fatal error ! Empty Filename');
  123.     Halt(1);
  124.   end;
  125. end;
  126.  
  127.  
  128. procedure user_display;
  129. type
  130.   TDayType = (Sun,Mon,Tue,Wed,Thu,Fri,Sat);
  131. var
  132.   YY        : Word = 0;
  133.   MM        : word = 0;
  134.   DD        : word = 0;
  135.   DayOfWeek : Word = 0;
  136.   hour      : Word = 0;
  137.   min       : Word = 0;
  138.   sec       : Word = 0;
  139.   msec      : Word = 0;
  140.   today     : TDayType;
  141. begin
  142.   textbackground(black);
  143.   ClrScr;
  144.   Getdate(YY,MM,DD,DayOfWeek);
  145.   GetTime(hour,min,sec,msec);
  146.   Today := TDayType(DayOfWeek);
  147.   textcolor(yellow);
  148.   WriteLn('Welcome to execute air traffic control');
  149.  
  150.   textcolor(yellow);
  151.   PromptForPassword('PassWord:', 'Wrong password, enter again');
  152.  
  153.   ClrScr;
  154.   WriteLn('Day of Today:',DD,'/',MM,'/',YY,',',Today);
  155.   WriteLn('Current time: ',hour,':',min,':',sec);
  156.   WriteLn('Now start the system of air traffic control management');
  157.  
  158.   textcolor(yellow);
  159.   PromptForPassword('PassWord:', 'Wrong password, enter again');
  160.  
  161.   ClrScr;
  162. end;
  163.  
  164.  
  165. procedure input_airtraffic(var aNrAirbornePlanes:integer);
  166. var n: integer;
  167. begin
  168.   for n:= Low(airtraffic) to High(airtraffic)
  169.     do airtraffic[n].airplane_no := '';
  170.  
  171.   aNrAirbornePlanes := 0;
  172.  
  173.   if not FileExists(airtrafficfilename) then
  174.   begin
  175.     CreateNewTextFile(airtrafficfilename, airtrafficfile);
  176.  
  177.     // AirTraffic must at least consist of one (valid) entry
  178.     // otherwise the rest of the code won't run properly as condition
  179.     // "zero planes in sky" plus "zero planes on ground" is treated as
  180.     // nothing to do (it might happen on startup so account for it here).
  181.     // ToDo: Add IOResult checking
  182.     Append(airtrafficfile);
  183.     WriteLn(airtrafficfile, 'fes2135487');
  184.     WriteLn(airtrafficfile, '3');
  185.     WriteLn(airtrafficfile, 'qwe7634153');
  186.     WriteLn(airtrafficfile, '7');
  187.     WriteLn(airtrafficfile, 'jfd1325748');
  188.     WriteLn(airtrafficfile, '11');
  189.     Close(airtrafficfile);
  190.   end;
  191.  
  192.   // ToDo: Add IOResult checking
  193.   Assign(airtrafficfile, airtrafficfilename);
  194.   Reset(airtrafficfile);
  195.  
  196.   // ToDo: Add IOResult checking
  197.   while not eof(airtrafficfile) do
  198.   begin
  199.     aNrAirbornePlanes := aNrAirbornePlanes + 1;
  200.     with airtraffic[aNrAirbornePlanes] do
  201.     begin
  202.       readln(airtrafficfile,airplane_no);
  203.       readln(airtrafficfile,airplane_landing_time_left);
  204.     end;
  205.   end;
  206.  
  207.   close(airtrafficfile);
  208. end;
  209.  
  210.  
  211. procedure input_platform_condition(var aNrLandedPlanes: integer);
  212. var
  213.   idx, n : integer;
  214. begin
  215.   Writeln('dumping airport plane stats');
  216.   aNrLandedPlanes :=0;
  217.   for n := Low(platform) to High(platform)
  218.     do platform[n].airplane_no := '';
  219.  
  220.   if not FileExists(platformfilename) then
  221.   begin
  222.     CreateNewTextFile(platformfilename, platformfile);
  223.   end;
  224.  
  225.   // ToDo: Add IOResult checking
  226.   Assign(platformfile, platformfilename);
  227.   Reset(platformfile);
  228.  
  229.   // ToDo: Add IOResult checking
  230.   idx := 0;
  231.   while not eof(platformfile) do
  232.   begin
  233.     if (idx < High(platform)) then inc(idx) else break;
  234.  
  235.     with platform[idx] do
  236.     begin
  237.       readln(platformfile,airplane_no);
  238.       readln(platformfile,airplane_predicted_time_left);
  239.  
  240.       if (airplane_no <> '') then
  241.       begin
  242.         aNrLandedPlanes := aNrLandedPlanes + 1;
  243.         Writeln('[ok] Plane = "', airplane_no, '" predicted_time_left = ', airplane_predicted_time_left);
  244.       end
  245.       else
  246.         Writeln('[xx] Plane = "', airplane_no, '" predicted_time_left = ', airplane_predicted_time_left);
  247.     end;
  248.   end;
  249.   close(platformfile);
  250.  
  251.   WriteLn('Press enter to continue'); ReadLn;
  252. end;
  253.  
  254.  
  255. procedure take_off(var aNrLandedPlanes,target:integer);
  256. var
  257.   choice: char;
  258. begin
  259.   with platform[target] do
  260.   begin
  261.     repeat
  262.       write('plane ',airplane_no,'is ready to take off,asking for permission(Y/N)?');
  263.       choice := ReadKey;
  264.       if not( Upcase(choice) in ['Y','N'] )
  265.       then write('Wrong input choice,please input choice again(Y/N):');
  266.       ClrScr;
  267.     until (Upcase(choice) In ['Y','N']);
  268.  
  269.     if  (UpCase(choice)='N')
  270.     then airplane_predicted_time_left := 5 else
  271.     begin
  272.       PromptForPassWord('Enter password to execute this order:');
  273.  
  274.       WriteLn('please wait for the processing of taking off.......');
  275.       Delay(TakeOffDelay);
  276.       writeln('plane ',airplane_no,'has been taken off');
  277.       ReadLn;
  278.       aNrLandedPlanes := aNrLandedPlanes - 1;
  279.       airplane_no := '$$$';
  280.       airplane_predicted_time_left := 999;
  281.     end;
  282.   end;
  283. end;
  284.  
  285.  
  286. procedure update_platform_time_left(var aNrLandedPlanes, target, count: integer);
  287. begin
  288.   target := 0;
  289. //@  if count <= High(platform) then
  290.   if count < High(platform) then
  291.   begin
  292.     count := count+1;
  293.     with platform[count] do
  294.     begin
  295.       airplane_predicted_time_left := airplane_predicted_time_left - 1;
  296.       if (airplane_predicted_time_left<=0) and (airplane_no <> '') then
  297.       begin
  298.         target := count;
  299.         if (platform[target].airplane_no <> '$$$') then take_off(aNrLandedPlanes, target);
  300.         if (count <= High(platform))
  301.         then update_platform_time_left(aNrLandedPlanes, target, count);
  302.       end
  303.       else if count < High(platform)
  304.            then update_platform_time_left(aNrLandedPlanes, target, count);
  305.     end;
  306.   end;
  307. end;
  308.  
  309.  
  310. procedure update_air_traffic(count:integer; var aNrLandedPlanes, aNrAirbornePlanes: integer);
  311. var
  312.   choice1: char;
  313.   target,count1, n: integer;
  314. begin
  315.   n := 1;
  316.   target := 0;
  317.   count1 := 0;
  318.   count := count+1;
  319.  
  320.   if count<=aNrAirbornePlanes then
  321.   begin
  322.     with airtraffic[count] do
  323.     begin
  324.       airplane_landing_time_left := airplane_landing_time_left-1;
  325.       if (airplane_landing_time_left<=0) and (airplane_no<>'') then
  326.       begin
  327.         repeat
  328.           write('airplane ',airplane_no,' is asking for the permission of landing(Y/N):');
  329.           {$IFDEF SKIP_CHOICEPOSITIVE}
  330.           choice1 := 'y';
  331.           {$ELSE}
  332.           choice1 := ReadKey;
  333.           {$ENDIF}
  334.         until UpCase(choice1) in ['Y', 'N'];
  335.         WriteLn;
  336.  
  337.         PromptForPassWord('Please input password to continue:');
  338.  
  339.         case Upcase(choice1) of
  340.           'Y':
  341.           begin
  342.             Writeln('chosen to allow landing, searching for a free platform');
  343.  
  344.             target := -1; // assume failure
  345.             for count1 := Low(platform) to High(platform) do
  346.             begin
  347.               if platform[count1].airplane_no = '' then
  348.               begin
  349.                 target := count1;
  350.                 break;
  351.               end;
  352.             end;
  353.  
  354.             // WriteLn('The target platform is ', target);
  355.             // WriteLn('The value of count1 is ', count1);
  356.  
  357.             if (target < 1) then
  358.             Begin
  359.               writeln('No platform remain, please stay on the air');
  360.               airplane_landing_time_left := 5;
  361.             end
  362.             else
  363.             begin
  364.               {$IFNDEF SKIP_WAITS}
  365.               write('airplane ',airplane_no, ' landing to platform ',target);
  366.               readln;
  367.               {$ENDIF}
  368.               platform[target].airplane_no := airplane_no;
  369.               platform[target].airplane_predicted_time_left := 400;
  370.               airplane_no := '';
  371.               aNrLandedPlanes := aNrLandedPlanes + 1;
  372.  
  373.               // Update airtraffic array, e.g. 'remove' item
  374.               for n := Succ(count) to High(AirTraffic) do
  375.               begin
  376.                 // copy/transfer
  377.                 airtraffic[Pred(n)].airplane_no                := airtraffic[n].airplane_no;
  378.                 airtraffic[Pred(n)].airplane_landing_time_left := airtraffic[n].airplane_landing_time_left;
  379.               end;
  380.               // Always 'clear' last item in list as things moved. This also
  381.               // takes care of things in case the above loop did not execute.
  382.               airtraffic[High(AirTraffic)].airplane_no := '';
  383.               airtraffic[High(AirTraffic)].airplane_landing_time_left := 0;
  384.  
  385.               // We now have one less plane in the air.
  386.               aNrAirbornePlanes := aNrAirbornePlanes - 1;
  387.             end;
  388. //@         aNrAirbornePlanes := aNrAirbornePlanes - 1;
  389.           end;
  390.           'N': airplane_landing_time_left := 5
  391.         end  { case choice }
  392.       end
  393.  
  394.       else
  395.  
  396. //@   if count <= High(airtraffic) then
  397.       if count < High(airtraffic) then
  398.       begin
  399.         update_air_traffic(count, aNrLandedPlanes, aNrAirbornePlanes);
  400.       end;
  401.     end;
  402.   end;
  403. end;
  404.  
  405.  
  406. procedure generate_airplane_no(var aNrAirbornePlanes: integer);
  407. Var
  408.   x: array[1..3] of char;
  409.   y: array[1..7] of char;
  410.   z: string[30] = '';
  411.   count, abc: integer;
  412. begin
  413.   if (aNrAirbornePlanes < High(airtraffic)) then
  414.   begin
  415.     aNrAirbornePlanes := aNrAirbornePlanes + 1;
  416.     for count:= Low(x) to High(x)
  417.       do x[count] := ' ';
  418.     for count:= Low(y) to High(y)
  419.       do y[count] := ' ';
  420.  
  421.     if aNrAirbornePlanes <= High(airtraffic) then
  422.     begin
  423.       randomize;
  424.       abc := random(9);
  425.       for count:= Low(x) to High(x)
  426.         do x[count] := chr(random(26)+65);
  427.       for count:= Low(y) to High(y)
  428.         do y[count] := chr(random(10)+48);
  429.       for count := 1 To 2
  430.         do z := concat(z, x[count]);
  431.       for count := low(y) to High(y)
  432.         do z := concat(z, y[count]);
  433.  
  434.       with airtraffic[aNrAirbornePlanes] do
  435.       begin
  436.         airplane_no := z;
  437.         airplane_landing_time_left := random(20)+ 1;  // ??? High(airtraffic) intended ?
  438.         {$IFNDEF SKIP_WAITS}
  439.         readln;
  440.         {$ENDIF}
  441.       end;
  442.     end;
  443.   end;
  444. end;
  445.  
  446.  
  447. procedure update_platformfile;
  448. var
  449.   n: integer;
  450. begin
  451.   rewrite(platformfile);
  452.   for n := Low(PlatForm) to High(Platform) do
  453.   begin
  454.     with platform[n] do
  455.     begin
  456.       writeln(platformfile,airplane_no);
  457.       writeln(platformfile,airplane_predicted_time_left);
  458.     end;
  459.   end;
  460.   close(platformfile);
  461. end;
  462.  
  463.  
  464. procedure update_airtrafficfile(var aNrAirbornePlanes: integer);
  465. var n: integer;
  466. begin
  467.   rewrite(airtrafficfile);
  468.   for n := Low(AirTraffic) to aNrAirbornePlanes do
  469.   begin
  470.     with airtraffic[n] do
  471.     begin
  472.       writeln(airtrafficfile, airplane_no);
  473.       writeln(airtrafficfile, airplane_landing_time_left);
  474.     end;
  475.   end;
  476.   close(airtrafficfile);
  477. end;
  478.  
  479.  
  480. begin
  481.   NrAirbornePlanes := 0;
  482.   user_display;
  483.   input_airtraffic(NrAirbornePlanes);
  484.   input_platform_condition(NrLandedPlanes);
  485.  
  486.   while (NrLandedPlanes >= 0) do
  487.   begin
  488.     ClrScr;
  489.     GetTime(hour,min,sec,msec);
  490.     writeln('Current time: ',hour,':',min,':',sec);
  491.     z := 0;
  492.     writeln('There are total ', NrLandedPlanes, ' planes in the airport now');
  493.     writeln('There are total ', NrAirbornePlanes, ' planes are waiting  for landing');
  494.     count := 0;
  495.     repeat
  496.       writeln('Press Enter to continue/Press ESC to exit');
  497.       ch := ReadKey;
  498.       ClrScr;
  499.     until (ch=#27) or (ch=#13);
  500.  
  501.     case ch of
  502.       #13:
  503.       begin
  504.         if ( (NrLandedPlanes=0) and (NrAirbornePlanes=0) ) then
  505.         begin
  506.           WriteLn('No plane information found, please wait until plane land/take off....');
  507.           WriteLn('press ENTER to continue!!');
  508.           repeat
  509.             ch := ReadKey;
  510.           until (ch=#13);
  511.           ClrScr;
  512.         end
  513.         else
  514.         begin
  515.           generate_airplane_no(NrAirbornePlanes);
  516.           ClrScr;
  517.           WriteLn('Waiting for the system instruction.....');
  518.           Delay(SystemInstructionDelay);
  519.           ClrScr;
  520.           update_air_traffic(count, NrLandedPlanes, NrAirbornePlanes);
  521.           update_platform_time_left(NrLandedPlanes, target, count);
  522.           update_platformfile;
  523.           update_airtrafficfile(NrAirbornePlanes);
  524.         end;
  525.       end;
  526.       #27: exit;
  527.     end;  { case ch }
  528.   end;
  529. end.
  530.  
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 12, 2016, 02:50:31 pm
I could not resist.

I could not put the source inside code-tags any more, the board gave me this error:
The message exceeds the maximum allowed length (20000 characters).

Attached air.zip contains sourcecode (air.lpr) and for use with Lazarus as editor also the .lpi.

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 12, 2016, 06:11:58 pm
Nice one Bart ! Thank you for the additions.

My idea about CharPrompt was to make it a true/false returning function and giving it true/false set of chars together with the prompt. Yours is doing very fine as well :)

I can only have admiration for the simplicity of the IOChecking implementation. Gosh, what we had to do for all those years to account for each and every possible failure. I haven't used simple textfiles like that for ages so, i was not sure how to approach. It makes me appreciate your implementation/solution the more.

I have one (unrelated to your code) question though: I noticed that code-tools in my lazarus seems to be tripping over the variable name platform. Did i miss it is/was a keyword or modifier somewhere ?
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 12, 2016, 10:55:56 pm
platform may very well be a modifier.

About IOChecking: notice that the whole block /procedure will halt the application on 1 single IOError.
Thank god for try..except/try..finally blocks.
In the old days it really was hard.

This piece of code can be rewritten in modern equivalents and it would be so much simpler.

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: howardpc on October 12, 2016, 11:03:54 pm
platform is indeed a modifier (a hint directive).
You can use it with impunity by escaping it with & though (&platform).
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 12, 2016, 11:27:36 pm
@Bart:
Yes i noticed it is in blocks. Well... there (imho) actually isn't a good solution to begin with because of the way things are implemented. I feel so restricted working with only fpc procedural basics. The code would greatly benefit when going oop or at least use some modern language features. Using advanced records alone would already help tremendously. iow, i agree fully with your opinion.


Ah. thank you for the confirmation howardpc (and also Bart). That explain things :)

At least i was able to find the documentation on that directive (http://www.freepascal.org/docs-html/ref/refse5.html) now. The ampersand indeed neatly gets rid of nasty side-effects.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 13, 2016, 06:39:37 pm
Since I have nothing better to do with my life  :D

A rewritten version of the code.
It uses TCustomApplication so it is easy to catch all exceptions in one place, and modern classes to load and save files.
Arrays are dynamical now.
Used one type for the record definition of an airplane.
File access is only on startup and close.
Procedures do what they say, they do not have trange by-effects.

Pseudo-code in the main file says what it is supposed to do.

Of course much room for improvement.

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 13, 2016, 10:45:46 pm
Small update: it does not require *.txt files at startup.

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 15, 2016, 05:05:57 pm
Because I'm pathetic  :-[

Some simplifications.
All defines now work, so it can be run in fully automatic mode (and you can still quit by pressing escape (and have some patience then)).

Now it's time to so something usefull  O:-)

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 16, 2016, 11:14:37 pm
Planes now carry fuell and will crash if they run out of it...

Bart
Title: Re: I am the new of pascal .please give me some HELP!
Post by: molly on October 16, 2016, 11:28:10 pm
drats, i was trying to implement something similar.

Are you that fast with programming or am i just simply that slow ? (that was rhetorical)  :D

Multiple airports and fly zones, passenger/cargo lists (for weight to calculating fuel loss), boarding schedules, runways and the occasional storm that needs to be flown around. Definitely something for going oop. It is all on the todo list but, unfortunately i have not much time to work on it  :(

Thank you very much for the additions Bart. very much appreciated to see a small hobby/learning project come to life/maturity.
Title: Re: I am the new of pascal .please give me some HELP!
Post by: Bart on October 17, 2016, 06:28:51 pm
In a perfect world I would like to make it message driven and introduce the AT-controller as a class. In that scenario messages can be sent airplane <-> controller.

With a little elbow-grease and sweat we wil eventually come up with the ultimate AT-control LGPL licensed software  8-)

The PlaneCrash method (and it's related message) should remain untouched though  >:D

For the moment I see 2 improvements:

Unfortunately my 1 week holiday is over, so my regular job east most of my energy.

Bart
TinyPortal © 2005-2018