Christopher Clarke
Christopher Clarke

Reputation: 169

Why does my program not output all my data?

program ZZX1;

{$mode objfpc}{$H+}

uses
  crt,
  wincrt,
  {$IFDEF UNIX}{$IFDEF UseCThreads}
  cthreads,
  {$ENDIF}{$ENDIF}
  Classes
  { you can add units after this  };
type
  Masquerader = record
    Name, CountyCode: string;
    Payment: real;
   end;
var
  Applicant: array[1..10] of Masquerader;
  DemList: array[1..10] of string;
  BerList: array[1..10] of string;
  EsqList: array[1..10] of string;
  x:integer;
  Y:integer;
  DemCounter:integer;
  BerCounter:integer;
  EsqCounter:integer;
  DemAmount:real;
  BerAmount:real;
  EsqAmount:real;

 procedure LoadData;
 begin
    clrscr;
X:=0;
DemCounter:=0;
BerCounter:=0;
EsqCounter:=0;
DemAmount:=0;
BerAmount:=0;
EsqAmount:=0;
repeat
X:= x+1;
repeat
write('Enter Your County Code DemM or BerM or EsqM: ');
readln(Applicant[x].CountyCode);
until (Applicant[x].CountyCode= 'DemM') or (Applicant[x].CountyCode= 'BerM') or (Applicant[x].CountyCode= 'EsqM');
If Applicant[x].CountyCode = 'DemM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    DemCounter:= DemCounter + 1;
    DemAmount:= DemAmount + Applicant[x].Payment;
    DemList[DemCounter]:= Applicant[x].Name;
  end;
If Applicant[x].CountyCode = 'BerM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    BerCounter:= BerCounter + 1;
    BerAmount:= BerAmount + Applicant[x].Payment;
    BerList[BerCounter]:= Applicant[x].Name;
  end;
If Applicant[x].CountyCode = 'EsqM' then
  begin
    write('Enter Your Name: ');
    readln(Applicant[x].Name);
    write('Enter Your Total Payment: ');
    readln(Applicant[x].Payment);
    clrscr;
    EsqCounter:= EsqCounter + 1;
    EsqAmount:= EsqAmount + Applicant[x].Payment;
    EsqList[EsqCounter]:= Applicant[x].Name;
  end;
until x=6 ;

 end;

Procedure PrintData;
  begin
    Y:= 0;
    for y := 1 to 6 do
    begin
      writeln('Name: ', Applicant[y].Name);
      writeln('CountyCode: ', Applicant[y].CountyCode);
      writeln('Payment: ', Applicant[y].Payment:0:2);
      writeln;
    end;
    For Y:= 1 to DemCounter do
      begin
        writeln(DemList[Y]);
        writeln(DemCounter,'',' persons are registered in Demerara');
        writeln;
        writeln('DemTotal:$ ', DemAmount:0:2);
      end;
    For Y:= 1 to BerCounter do
      begin
        writeln(BerList[Y]);
        writeln(BerCounter,'',' persons are registered in Berbice');
        writeln;
        writeln('BerTotal:$ ', BerAmount:0:2);
      end;
    For Y:= 1 to EsqCounter do
      begin
        writeln(EsqList[Y]);
        writeln(EsqCounter,'',' persons are registered in Essequibo');
        writeln;
        writeln('EsqTotal:$ ', EsqAmount:0:2);
      end;
 end;
Procedure quit;
  begin
    writeln('Press <Enter> To Quit');
    readln;
  end;

begin
  LoadData;
  PrintData;
  quit;
end.

This program currently collects 6 persons and groups them by their countycode, calculating the total amount of persons and money collected by each county. When I run the program below my expected output is on the screen for a few seconds then it disappears leaving only a piece of the expected output( The end Part). Please assist.

Upvotes: 2

Views: 830

Answers (1)

Simon
Simon

Reputation: 10841

If there are characters in the keyboard buffer when the program reaches the readln; statement in the procedure quit, readln will read those characters and continue onwards rather than waiting for further input before continuing.

To check this, try adding a character variable as a parameter to readln and write the ASCII value of the character out (or check its value in a debugger) to see if there is anything in that variable after the readln.

(EDIT)

After further thinking, I wonder if the code like:

For Y:= 1 to EsqCounter do
  begin
    writeln(EsqList[Y]);
    writeln(EsqCounter,'',' persons are registered in Essequibo');
    writeln;
    writeln('EsqTotal:$ ', EsqAmount:0:2);
  end;

... should actually read something like:

For Y:= 1 to EsqCounter do
  begin
    writeln(EsqList[Y]);
  end;
writeln(EsqCounter,'',' persons are registered in Essequibo');
writeln;
writeln('EsqTotal:$ ', EsqAmount:0:2);

... because otherwise the same values of EsqCounter and EsqTotal will be output EsqCounter times, which seems unnecessary.

Upvotes: 2

Related Questions