Recent

Author Topic: Pascal Program  (Read 4186 times)

Txnman

  • Newbie
  • Posts: 4
Pascal Program
« on: March 12, 2015, 09:02:35 pm »
Program Txnman_Studies;
uses crt;


  Const
Payment = 1050 ;
Var
Name,Trn:String;
Y2,M6,Currentpoints,Y2Total,M6Total,S1Total,S1:Integer;
Cost:Real;

Begin
Writeln('Made by TnxMan');
Y2:=0;
M6:=0;
S1:=0;

Repeat
  writeln('enter a name');
  readln(name);
  writeln('enter TRN');
  readln(TRN);
  Writeln('enter Currentpoints');
  readln(currentpoints);

  if currentpoints>=19 then
Begin
  writeln('suspended 2 years');
  Y2:=Y2+1;
  Y2Total:=Y2Total+ Cost;
  Cost:= Payment * Currentpoints;
  Writeln('Offender',Name);
  Writeln('Cost of Ticket',Cost);
  End;
  if (Currentpoints>=10) and (Currentpoints<= 13) Then
Begin
   Writeln('6 month suspension');
   M6:=M6+1;
   Cost:= Payment * Currentpoints;
   M6Total:=M6Total+Cost;
   Writeln('Offender',Name);
   Writeln('Cost of Ticket',Cost);
  End;
  if (Currentpoints>=14) and (Currentpoints<19) then
Begin
   writeln('suspended 1 year');
   S1:=S1+1;
   Cost:= Payment * Currentpoints;
   S1:=S1Total+Cost;
   Writeln('Offender',Name);
   Writeln('Cost of Ticket',Cost);
   End;
   Until Currentpoints ='999';

    Writeln ('count of suspension 2 years',Y2);
    Writeln ('count of suspension 6 months',M6);
    Writeln ('count of suspension 1 year',S1);
    Writeln ('2 years collection',  Y2Total);
    Writeln ('6 months collection',M6Total);
    Writeln ('1 year collection',S1Total);
    Readkey;
End.

I've been getting this 2 errors
57 / 10 progra~1.pas
 Error: Incompatible types: got "LONGINT" expected "SHORTSTRING" with  Until Currentpoints >'999'; Highlighted.
And the other error is
63 pascal~1.pas
 Fatal: There were 1 errors compiling module, stopping


Mine helping me find the error? :)


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Pascal Program
« Reply #1 on: March 12, 2015, 09:15:34 pm »
You declared currentpoints of type string and later you compared it with a number
Code: [Select]
  if currentpoints>=19 then

A few possible solutions:
Change it to a suitable type.
Convert its contents from string to number in a different variable.
Or change the number to string.

Nitorami

  • Hero Member
  • *****
  • Posts: 605
Re: Pascal Program
« Reply #2 on: March 12, 2015, 09:58:01 pm »
The other way round:

You declare CurrentPoints as integer and then compare it to a string.

And there is a number of other errors such as

  Y2Total:=Y2Total+ Cost;

where Y2Total is an integer and cost is a real. You need to make Y2Total a real if you want to add a real.

Txnman

  • Newbie
  • Posts: 4
Re: Pascal Program
« Reply #3 on: March 13, 2015, 02:12:24 am »
i dont understand  :'(

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: Pascal Program
« Reply #4 on: March 13, 2015, 02:34:18 am »
57 / 10 progra~1.pas
 Error: Incompatible types: got "LONGINT" expected "SHORTSTRING" with  Until Currentpoints >'999';
57 is the line number. Go to that line, and see why it expects ShortString (text) and why you it has a LongInt (number).

Eugene Loza

  • Hero Member
  • *****
  • Posts: 729
    • My games in Pascal
Re: Pascal Program
« Reply #5 on: March 13, 2015, 10:10:57 am »
Quote
i dont understand  :'(

Code: [Select]
...Currentpoints...:Integer;
...
Until Currentpoints ='999';
Currentpoints is declared as integer and you try to compare it to a string '999'. Either declare currentpoints as a string, or make 999 not a string, but an integer.

Code: [Select]
S1:=S1Total+Cost;S1Total has no assigned value.
Code: [Select]
Y2Total:=Y2Total+ Cost;Y2Total is an integer and Cost is real - these types are incompatible. Moreover Cost is not initialized the first time.

Try to stay closer to the program logic. It's easy to make the errors when not thinking the way 'first of all I should calculate 1,2,3,4,5... then, based on these values I can calculate 7,8,9...' All values used in the calculations must be initialized, or you'll face strange bugs. And, the easier part, always check declared types to be compatible. E.g. you must render a string as a number before comparing it to a number, or making any mathematical operations with it. So goes for the 'real' type. Convert it to integer, or convert the whole expression to real. But do not mix them.
My FOSS games in FreePascal&CastleGameEngine: https://decoherence.itch.io/ (Sources: https://gitlab.com/EugeneLoza)

eny

  • Hero Member
  • *****
  • Posts: 1665
Re: Pascal Program
« Reply #6 on: March 13, 2015, 11:36:49 am »
At least this compiles.
Now compare it with your pseudo code to see if it works.

Code: [Select]
program tnxman_homework_help;

const
  Payment = 1050 ;

var
  Name, Trn: string;
  Y2, M6, S1, Currentpoints, Y2Total, M6Total, S1Total:integer;
  Cost: integer;

begin
  writeln('Made by TnxMan');
  Y2 := 0;
  M6 := 0;
  S1 := 0;

  repeat

    writeln('enter a name');        readln(name);
    writeln('enter TRN');           readln(TRN);
    writeln('enter Currentpoints'); readln(currentpoints);

    if (Currentpoints >= 10) and (Currentpoints <= 13) Then
    begin
       M6 := M6 + 1;
       Cost := Payment * Currentpoints;
       M6Total := M6Total + Cost;

       writeln('6 month suspension');
       writeln('Offender', Name);
       writeln('Cost of Ticket', Cost);
      end;

    if (Currentpoints >= 14) and (Currentpoints <= 18) then
    begin
       S1 := S1 + 1;
       Cost := Payment * Currentpoints;
       S1 := S1Total + Cost;

       writeln('suspended 1 year');
       writeln('Offender', Name);
       writeln('Cost of Ticket', Cost);
     end;

    if currentpoints >= 19 then
    begin
      Y2 := Y2+1;
      Y2Total := Y2Total+ Cost;
      Cost :=  Payment * Currentpoints;

      writeln('suspended 2 years');
      writeln('Offender', Name);
      writeln('Cost of Ticket', Cost);
    end;

  until Currentpoints = 999;

  writeln ('count of suspension 2 years',Y2);
  writeln ('count of suspension 6 months',M6);
  writeln ('count of suspension 1 year',S1);
  writeln ('2 years collection',  Y2Total);
  writeln ('6 months collection',M6Total);
  writeln ('1 year collection',S1Total);

  writeln('Press Enter to end...');
  readln;
end.
« Last Edit: March 13, 2015, 11:38:35 am by eny »
All posts based on: Win11; stable Lazarus 4_4  (x64) 2026-02-12 (unless specified otherwise...)

 

TinyPortal © 2005-2018