Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Pascal data type checking

Posted on 2000-03-14
5
Medium Priority
?
371 Views
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
0
Comment
Question by:adrianmak
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 3
  • 2
5 Comments
 
LVL 5

Accepted Solution

by:
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;
var
  s :string;
  n :integer;
  code :integer;
begin
  write ('Enter an integer: ');
  readln(s);
  val(s, n, code);
  if code <> 0 then
    writeln('Not an integer.  Error at position: ', code);
  else
    writeln('the integer you entered is ',n);
  end;

end.  




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

Author Comment

by:adrianmak
ID: 2618762
scrapdog,

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

Expert Comment

by:scrapdog
ID: 2618775
val should work with both integers and reals

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

Expert Comment

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

Author Comment

by:adrianmak
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
0

Featured Post

Enroll in September's Course of the Month

This month’s featured course covers 16 hours of training in installation, management, and deployment of VMware vSphere virtualization environments. It's free for Premium Members, Team Accounts, and Qualified Experts!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

We live in a world of interfaces like the one in the title picture. VBA also allows to use interfaces which offers a lot of possibilities. This article describes how to use interfaces in VBA and how to work around their bugs.
By default Outlook 2016 displays only one time zone in the Calendar. The following article explains how to display two time zones in one calendar view.
Video by: ITPro.TV
In this episode Don builds upon the troubleshooting techniques by demonstrating how to properly monitor a vSphere deployment to detect problems before they occur. He begins the show using tools found within the vSphere suite as ends the show demonst…
Sometimes it takes a new vantage point, apart from our everyday security practices, to truly see our Active Directory (AD) vulnerabilities. We get used to implementing the same techniques and checking the same areas for a breach. This pattern can re…

722 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