954,529 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Have something to say? Contribute New Article Reply to this Article

Pascal starter.

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.

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

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?

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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

kc0arf
Posting Virtuoso
Team Colleague
1,937 posts since Mar 2004
Reputation Points: 121
Solved Threads: 57
 

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.

red_evolve
Posting Whiz
313 posts since Jun 2003
Reputation Points: 53
Solved Threads: 1
 

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

nikkisboricua
Light Poster
36 posts since Oct 2004
Reputation Points: 10
Solved Threads: 0
 
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.
Jackrabbit
Light Poster
31 posts since Jan 2005
Reputation Points: 10
Solved Threads: 0
 

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).

jwenting
duckman
Team Colleague
8,392 posts since Nov 2004
Reputation Points: 1,662
Solved Threads: 337
 

This article has been dead for over three months

Post: Markdown Syntax: Formatting Help
You