Pascal data type checking

Posted on 2000-03-14
Medium Priority
Last Modified: 2010-04-16
In pascal, when I defined a integer data type and use a readling statement to get data from user input,
there is a run-time error if the user input characters but not numeric
so how do I manual checking the user input is the correct data type I defined
Question by:adrianmak
  • 3
  • 2

Accepted Solution

scrapdog earned 100 total points
ID: 2618588
a quick fix is to readln into a string, and then convert the string to an integer using the val procedure

the val procedure will do error checking for you

program foo;
  s :string;
  n :integer;
  code :integer;
  write ('Enter an integer: ');
  val(s, n, code);
  if code <> 0 then
    writeln('Not an integer.  Error at position: ', code);
    writeln('the integer you entered is ',n);


The val procedure takes three parameters.  The first is a string that you wish to convert to an integer.  The second is the integer variable you want to store it in.  The third is the variable you want to store the "error" code in.  If the code is 0, the conversion took place successfully.  If it is not zero, the code variable will hold the index of the position in the string where the error occurred (such as an alphabetic character inside the string, etc.).

The reason for doing it this way is so you can perform you own error handling...a run-time error is unlikely to result regardless of what you enter into a string variable.

Author Comment

ID: 2618762

It works but it seems only work for integer but not include real numbers

Expert Comment

ID: 2618775
val should work with both integers and reals

are you sure you set the type of your number variable to "real"?

Expert Comment

ID: 2618779
(the "code" variable must always be an integer though)

Author Comment

ID: 2618923
it works now.

It is depend on the data type of the variable n
if it define am integer then if i input a real number e.g. 5.6
it return of val is false
if defined as real it is true

Featured Post

Train for your Pen Testing Engineer Certification

Enroll today in this bundle of courses to gain experience in the logistics of pen testing, Linux fundamentals, vulnerability assessments, detecting live systems, and more! This series, valued at $3,000, is free for Premium members, Team Accounts, and Qualified Experts.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Join & Write a Comment

There are more than 1000 cryptocurrencies available across the world, but Bitcoin has gained a significant advantage over others. The cryptocurrencies are capitalizing the market with more than $150 billion dollars.
This article describes and provides a custom-made tool I wrote to give businesses a means of identifying commercial music content, without having to expend too much effort. Business recordings are easily identified from possibly illegal music files …
If you are looking for an automated solution for backup single or multiple Office 365 user mailboxes to Outlook data file, then you can use Kernel Office 365 Backup & Restore tool. Go through the video to check out the steps to backup single or mult…
When you have multiple client accounts to manage, it often feels like there aren’t enough hours in the day. With too many applications to juggle, you can’t focus on your clients, much less your growing to-do list. But that doesn’t have to be the cas…

624 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question