Checking real numbers 
Author Message
 Checking real numbers

Quote:
> Its true that when you try and enter a letter into a real variable, the
> program crashes....

> how do you go about stopping this.

> i need a way to check the values before they are entered into the variable
> so  that the program doesn't crash

   Pascal doesn't provide runtime error traps like some other languages,
but TP/BP overcome this inadequacy by allowing you, the programmer, to do
some of the work in I/o processing and intercept the errors during the
(string-to-numeric) conversion process.  Simply put, you can read string
data and use Val to convert it to a target numeric data type variable.  
If you look up the Val procedure, you'll see how it works, as well as how
to use the "res" parameter to see if any error occurred in the conversion
process.
   This is what most of us do when facing your problem: use Val.


Wed, 18 Jun 1902 08:00:00 GMT  
 Checking real numbers
Its true that when you try and enter a letter into a real variable, the
program crashes....

how do you go about stopping this.

i need a way to check the values before they are entered into the variable
so  that the program doesn't crash

PLEASE HELP!!



Wed, 18 Jun 1902 08:00:00 GMT  
 Checking real numbers
On Sat 10 Jun 2000 12:26:24a,  In article

Quote:

>> Its true that when you try and enter a letter into a real variable,
>> the program crashes....

>> how do you go about stopping this.

>> i need a way to check the values before they are entered into the
>> variable so  that the program doesn't crash

>   Pascal doesn't provide runtime error traps like some other languages,

Try this:

-----------begin-------------
var
x: double;
rt: integer;

begin

write('enter a number: ');

{$i-}
readln(x);
rt:=ioresult;
{$i+}

writeln(rt,' ',x);

end.
------------end----------------

rt will return a non-zero value if an error occurs



Wed, 18 Jun 1902 08:00:00 GMT  
 Checking real numbers
On Sat, 10 Jun 2000 01:19:25 +1000, "Scott Harris"

Quote:

>Its true that when you try and enter a letter into a real variable, the
>program crashes....

>how do you go about stopping this.

>i need a way to check the values before they are entered into the variable
>so  that the program doesn't crash

>PLEASE HELP!!

Mostly I use VAL as Mike Copeland suggests. In some applications it is
convenient to prevent crashes this way:

Program RealInput;
{Using compiler directive to catch wrong keyboard entries}

VAR x:Real;

Begin
Writeln('ENTER 0 TO QUIT':30);
Repeat
     Repeat
          Write('Input real: ');
          {$I-}
          Readln(x);
          {$I+}
     Until IOResult = 0;
     Writeln(x:1:5);
Until x = 0;
END.



Wed, 18 Jun 1902 08:00:00 GMT  
 
 [ 4 post ] 

 Relevant Pages 

1. Real number error checking?

2. String number to Real number

3. How convert REAL type stored in six bytes to number

4. Delphi databases - real numbers

5. How to get REAL numbers in DBGRid to show like currency

6. Rounding off a real number to half thousands

7. Rounding real number to half thousands

8. Real number treated as Integers?

9. Problem with real numbers - labour.zip [0/1]

10. Problem with real numbers - labour.zip [0/1]

11. pascal integers and real numbers

12. Writing real numbers to file in hex format

 

 
Powered by phpBB® Forum Software