Search code examples
repeatpascal

Even when constant is define as 1 to maxint , 0 is still accepted in Pascal


I'm confused because when I define a type from 1 to maxint in Pascal and I make the choice "0" which should return back to the repeat loop. Here is my code:

program Tanken;

type
tZahl = 1..maxint; 

var
tag1 : tZahl;
wahl : tZahl;
liter,
p : real;
BEGIN
repeat
    write ('Bitte Tag eingeben 1=Mon 2=Die 3=Mit 4=Don 5=Fre 6=Sam 7=Son: ');
    readln (tag1);
    writeln(tag1);
        
until tag1 <= 7;

    ....
end

This is how my constant, type and variable looks. Si I define tag1 as tZahl which should be from 1 to maxint but how ever when I run this at the first repeat loop when I type "0" it is accepted. I found this a bit confusing any ideas?


Solution

  • To force type range checking at runtime you need to explicitly tell the compiler to with most used compilers by adding {$R+} to the top of the program.

    However this will only throw a runtime error, which is not the input validation that you want. You will really need to program input validation yourself. E.g. by reading a string, and then converting it to a number using the VAL() procedure and checking the code argument for errors.