Board index » delphi » runtime errors

runtime errors

My program asks the user for some numbers, but if hes stupid enough
to write for instance "hello" instead of real numbers, I get a runtime
error.. How can I avoid errors and instead ask the user to rewrite it
in numbers instead..

Stefan Ottosson, Stockholm, Sweden  
Email: ottos...@canit.se            

 

Re:runtime errors


In article <46b33v$...@uno.canit.se>,
Quote
Stefan Ottosson <ottos...@canit.se> wrote:

:My program asks the user for some numbers, but if hes stupid enough
:to write for instance "hello" instead of real numbers, I get a runtime
:error.. How can I avoid errors and instead ask the user to rewrite it
:in numbers instead..

This is one of the most elementary tricks in the trade. Read the
input into a string and then convert with Val into a number while
using Val's error checking. For the details of the syntax please
consult your manual.

   All the best, Timo

....................................................................
Prof. Timo Salmi   Co-moderator of news:comp.archives.msdos.announce
Moderating at ftp:// & http://garbo.uwasa.fi archives  193.166.120.5
Department of Accounting and Business Finance  ; University of Vaasa
t...@uwasa.fi http://uwasa.fi/~ts BBS 961-3170972; FIN-65101,  Finland

Re:runtime errors


Quote
Stefan Ottosson (ottos...@canit.se) wrote:

: My program asks the user for some numbers, but if hes stupid enough
: to write for instance "hello" instead of real numbers, I get a runtime
: error.. How can I avoid errors and instead ask the user to rewrite it
: in numbers instead..

TP 7.0 Programmer's reference page 98

You turn off autmatic I/O checking by using the following complier directive>
{I-}

However, after every I/O function you must look at the result. (After an
errot all subsequent I/O will be ignored until you check IOResult) example:

... {begining of program, variable decleration etc.}

{I-}
write ('Enter an integer only!: ');
readln(intvar);
if IOResult = 0 then writeln ('You can follow directions!')
else writeln ('What are you trying to do? Crash my program?');
{I+}
...

{I+} turn error checking back on...

I take no responsibility for this code..But if I said something wrong, I
have no fear of being corrected...

TTYL
Josh

Re:runtime errors


Quote
>My program asks the user for some numbers, but if hes stupid enough
>to write for instance "hello" instead of real numbers, I get a runtime
>error.. How can I avoid errors and instead ask the user to rewrite it
>in numbers instead..

>Stefan Ottosson, Stockholm, Sweden  
>Email: ottos...@canit.se            

One solution is to read the user's input as text and convert it into a real
number.

Jose' Perez

Other Threads