8

I'm just wondering how it's possible to do type checking in pascal? I have been searching for hours now but I haven't been able to find anything useful.

Example:

var 
number: Integer;

begin
  write('Enter a number: ');
  read(number);

  if {How am I supposed to check if 'number' is an Integer here?}
  then writeln(number)
  else writeln('Invalid input')
end.
Radix
  • 1,317
  • 2
  • 17
  • 32

6 Answers6

6

You are actually hitting the I/O type checking. You can work around this by disabling it temporarily and then checking the result:

 {$I-}  //turn off IO checking temporarily
 read(i);
 {$I+}  // and back on

 if ioresult=0 then  // check the result of the last IO operation
   writeln('integer successfully read:',number)
 else
   writeln('invalid input');

Note: the typical answer is often "just read a string and do the conversion yourself", however it is difficult to do that nicely without making assumptions about the terminal type.

For clear and simple programs where you just want somewhat validated input, the above trick (and a loop around it that repeats on error) is enough.

Marco van de Voort
  • 25,628
  • 5
  • 56
  • 89
2

Maybe Val procedure can help you. Here is one for fpc. But change your logic to read into a String and validate it using Val. You can find a sample here.

tenorsax
  • 21,123
  • 9
  • 60
  • 107
1

That 's too easy, see my code below:

program int_check;
uses crt;
var n:real;
begin 
     clrscr;
     write('Enter a number: ');readln(n);
     if n-round(n)=0 then write('Integer!') else write('Not an Integer!');
     readln;
end.

You see, no string, no IOcheck, and fits your form!

Pent Ploompuu
  • 5,364
  • 1
  • 27
  • 47
0

Since number is an Integer, the app will fail if the user types a non-numeric value. You'll never reach the if statement.

StilesCrisis
  • 15,972
  • 4
  • 39
  • 62
0

Use frac(n) directly

program int_check; uses crt; var n:real; begin clrscr; write('Enter a number: ');readln(n); if frac(n)=0 then write('Integer!') else write('Not an Integer!'); readln; end.

-2

The question is quite old, but maybe this code will help someone.

function GetVariableType (StringToCheck: String): String;
var
  xInteger: Integer;
  xBoolean: Boolean;
  xDouble: Double;
  xVarType: String;
begin
  xVarType:='VARCHAR';
  if TryStrToBool(StringToCheck,xBoolean) then xVarType:='BOOLEAN';
  if TryStrToFloat(StringToCheck,xDouble) then xVarType:='FLOAT';
  if TryStrToInt(StringToCheck,xInteger) then xVarType:='INTEGER';
  
  GetVariableType := xVarType;
end;

Of course, this can be refactored to return a boolean value.

  • The 'GetVariableType' answer is extremely heavily dependent upon functions provided by one brand of Pascal compiler ... and, I have a suspicion that they might fail to notice some kinds of incorrect data. In general, the answer to this (for Pascal, C, and other similar languages) to write (or find) your own parser. Such a parser would take a string of characters and look at them, and return back a magic value telling you: - is it a number, a string, a character, a word, empty, or unknown - if a number, it might return the 32-bit or 64-bit value in a second variable. - if a string, it might r – Stan Sieler Mar 10 '23 at 06:34
  • Of course, this is a very basic function that may not be enough. I use this function to find out what type of data is in each column of a CSV file to create a SQL import string. So far it hasn't failed me and the data has always been prepared and imported correctly. – jirihunacekcz Mar 12 '23 at 09:49