# Board index » delphi » HELP: Integer problem

## HELP: Integer problem

While working on a program, i ask the user to enter a number,
but if the user enters a letter it crashes.

eg (a slightly more basic example);

var
i : integer;

begin
writeln ('Enter a number');
writeln ('you wrote',i,');
end.

So how do i make it so the user has to enter a number????
Help would be accepted with many gratious thankyous ;)

See ya

## Re:HELP: Integer problem

##### Quote
Sir Fuzzer (Fuz...@nelsun.gen.nz) wrote:

: While working on a program, i ask the user to enter a number,
: but if the user enters a letter it crashes.
[cut]
: So how do i make it so the user has to enter a number????
: Help would be accepted with many gratious thankyous ;)

VAR
st : STRING;
num, err : INTEGER;
BEGIN
REPEAT
BEGIN
Write ('gimmi a number, please!');
Val (st, num, err);
END
UNTIL err = 0;
END;

I can't check the source, so there may be errors, but I think, you'll find
out what it's all about anyway.

--

CU Peter

-------------------------------------------------------------------
Peter Gritsch                              pe...@sbox.tu-graz.ac.at

There was a point to this story, but it has temporarily escaped the
chronicler's mind.
Douglas Adams, "So Long, and Thanks for all the Fish"

## Re:HELP: Integer problem

##### Quote
Sir Fuzzer <Fuz...@nelsun.gen.nz> wrote:
>While working on a program, i ask the user to enter a number,
>but if the user enters a letter it crashes.
>So how do i make it so the user has to enter a number????
>Help would be accepted with many gratious thankyous ;)

A solution with val as illustrated before would work or something
(untested) like this may work...

In ascii, 0-9 are represented as 48-57...you may call the ord function
on the character and see if it's between 48-57.....

## Re:HELP: Integer problem

In article <51r844\$...@fstgal00.tu-graz.ac.at>,
pe...@sbox.tu-graz.ac.at (PETER GRITSCH) wrote:

##### Quote
>Sir Fuzzer (Fuz...@nelsun.gen.nz) wrote:
>: While working on a program, i ask the user to enter a number,
>: but if the user enters a letter it crashes.
>[cut]
>: So how do i make it so the user has to enter a number????
>: Help would be accepted with many gratious thankyous ;)

>VAR
>   st : STRING;
>   num, err : INTEGER;
>BEGIN
>   REPEAT
>   BEGIN
>      Write ('gimmi a number, please!');
>      Val (st, num, err);
>   END
>   UNTIL err = 0;
>   ReadNumber := num;
>END;

>I can't check the source, so there may be errors, but I think, you'll find
>out what it's all about anyway.

Or an alternative is to disable the IO checking, such as you could do if
looking for a file on disk (or other IO). This means that the result placed
into num will be of the type defined for that variable. So, in our case, the
num will be of type integer.

cheers, Alex.

VAR
num : INTEGER;
done : BOOLEAN;
BEGIN
done:=false;
REPEAT
BEGIN
Write ('gimmi a number, please!');
{\$I-} ReadLn (num); {\$I+}
if IOResult = 0 then done:=true;
END
UNTIL done;
END;

## Re:HELP: Integer problem

##### Quote
>begin
>  writeln ('Enter a number');
>  writeln ('you wrote',i,');
>end.

Ok what your after is :

Begin
Repeat
Write('Enter A Number  :');
Until i in [0..9];

WriteLn('You Typed : ',i);
End;

Dark Knight

## Re:HELP: Integer problem

##### Quote

>Ok what your after is :

>Begin
>  Repeat
>    Write('Enter A Number  :');
>  Until i in [0..9];

>  WriteLn('You Typed : ',i);
>End;

>Dark Knight

no,  no, no.

The above code will still die if a non numeric is entered.

var
st : string;
i : integer;
code : integer;
done : boolean;
good : boolean;
begin
done := false; { loop termination}
good := false; { I contains valid value }
repeat
Write(' Numeric Entry Please?');
if st = '' then done := true; { Enter Exits Loop}
val(st,i,code);
if code = 0 then begin
done := true;
good := true;
end
else begin
Writeln(' Entry was not numeric! ');
Writeln(st);
while code > 1 do begin
write(' ');
dec(code);
end;
writeln('^invalid character');
end;
until done;
If good then Begin
Writeln(' The Number Entered was ',i);
end;
end.

Chuck Todd
at Warp with Speed Pascal/2