I get this error on my insertion sort algorithm:
insertionsort.lpr(19,17) Error: Incompatible types: got «Boolean» expected «LongInt»
Here’s the line 19 of my code
I have tried googling all over the internet but i couldn’t find any syntax errors or anything.
Here’s the full code if it helps :
I also get the same error on the bubble sort algorithm i did. Here’s the error line
bubblesort.lpr(26,14) Error: Incompatible types: got «Boolean» expected «LongInt»
Here’s line 26 of my algorithm:
Here’s the full code:
1 Answer 1
In Pascal, boolean operators and and or have higher precedence than the comparison operators > , = , etc. So in the expression:
Given that and has higher precedence, Pascal sees this as:
0 and A[j] are evaluated as a bitwise and (since the arguments are integers) resulting in an integer. Then the comparison, j > (0 and A[j]) is evaluated as a boolean result, leaving a check of that with > key , which is boolean > longint . You then get the error that a longint is expected instead of the boolean for the arithmetic comparison.
The way to fix it is to parenthesize:
The same issue applies with this statement:
which yields an error because or is higher precedence than = . So you can parenthesize:
Or, more canonical for booleans:
When in doubt about the precedence of operators, parenthesizing is a good idea, as it removes the doubt about what order operations are going to occur.
var a, b, c, d, e :integer;
begin
readln (a);
b:= a mod 10;
c:= a mod 100 — b / 10;
d:= a div 100;
e:= b+c+d;
writeln (e);
end.
Error: Incompatible types: got «Extended» expected SmallInt.
Ашипка: Неподходящие типы: получил «Extended» вместо ожидаемого «SmallInt» 🙁
var a, b, c, delta, x1, x2 : integer
writeln(‘Tell me the value of a.’);
writeln(‘Tell me the valeu of b.’);
writeln(‘Tell me the value of c.’);
delta := (SQR(b) — 4*a*c);
2 Answers
The problem is caused by using floating point arithmetic when x1 is an integer.
Try using one of the following options:-
1. Use round(), floor(), trunc() type rounding functions
code becomes: «x1 := Round( (-b + SQRT(delta)) / 2*a );»