Hi.

First of all, I'd like to apologize for I really do not know where to post this thread to. :lol: I know Pascal is out but apparently I have got to pick up this language in order to finish my project.

What I'd like to ask is whether Pascal offers exception handling. I'd like to read an integer but I've to consider the chances of users keying in a non-integer value. I've checked different sites which offer free online tutorial but to no avail. Please help. Thank you.

Hello,

I am not certain if this thread is alive or not, but if you are still out there, Please post your code and let's look at this. I enjoyed Pascal a lot in college, and wish I could still use it instead of C/C++ when I have to occasionally code.

Christian

Greetings.
A thousand apologies.
I was browsing through some old threads and I accidentally bump into my own old thread. :cheesy:
I had eventually gave up on exception handling because I still cannot figure out the way. However, if you know, I'd like to learn it. Thanks a lot.

Program FactorialCalculator;

uses crt;
var input : integer;

function calculate(num:integer):integer;
begin
if ((num = 0) or (num = 1)) then
calculate := 1
else
calculate := calculate(num-1) * num;
end;

begin
writeln;
writeln;
writeln('*** FACTORIAL CALCULATOR ***');
writeln('Enter integer: ');
readln(input);
writeln('The factorial of ',input,' is ',calculate(input));

end.

That was a small program that I got to write using Pascal as a starter.
I'd like to handle the exception where the prompt for an input from the user goes. I only want an integer here.
Any idea?

Hello,

Unfortunately, my Pascal things are in another city. Grr. What happens if you input a letter? Does the program substitute the ASCII value of the letter instead? Hmm. I am wondering if you should accept a string, and then convert it to numeric. I have to see if that function is even available in Pascal.

Wow, I am out of date here.

Christian

Greetings.

Alright. It's ok. I'm just glad that after so long, there is actually a reply from someone.

I'm in my office right now ( Industrial Training :sad: ). I'll check with my pascal compiler once I get home later in the night.

What happens if you input a letter? Does the program substitute the ASCII value of the letter instead?

Thanks anyway, Christian.

hey, im a pascal starter too. instead of this:

var input : integer;

try using this :

var input :real;

yea. i just learned that last week.

integer is a whole number
real is a whole or decimal. but if you put a decimal, REMEMBER to enter it like so: 0.6

program FactorialCalculator;

{$APPTYPE CONSOLE}

uses
   SysUtils;

var
   input : string;
   value : Integer;

function calculate( num : integer ) : integer;
begin
   if ( ( num = 0 ) or ( num = 1 ) ) then
      calculate := 1
   else
      calculate := calculate( num - 1 ) * num;
end;

begin
   writeln;
   writeln;
   writeln( '*** FACTORIAL CALCULATOR ***' );
   repeat
      write( 'Enter integer: ' );
      readln( input );
   until TryStrToInt( input, value );
   writeln( 'The factorial of ', input, ' is ', calculate( value ) );
end.

Whether you get exception handling or not depends on the flavour of Pascal you're working with.

Delphi (which is Pascal with a nice IDE and stuff) for example has a comprehensive exception mechanism (though it might not have been available in the very early versions, I don't have those here to check).

Other dialects may have duplicated this over time (Borland being the de-facto standard for Pascal in lue of an official standards body).

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.