On 06 Dec 97 Majed Al-Haj said to All...
MA> Seconed probelm..
MA> in the VAR section write this
MA> Number:INTEGER;
MA> READ(NUMBER);
MA> if i enter a char. like this "dd" and not Number like "44" take me
MA> this message "RUN-TIME ERROR" and exit form the program.
MA> How i can make the program accept just Number and don't accept and
MA> Char. --
Since you head your question "PASCAL", here is a Pascal reply - Hope Gary
won't say we are too far off-topic.
I like to treat all entries as strings, filtering bad keystrokes, and
converting them to numbers only when I know that will work. Here is an
example. To avoid re-writing the validation code for each field, we make a
function of it. You can use it again and again in program after program.
PROGRAM Temp;
USES crt, DOS;
Function GetInteger(var Num : LongInt ; x, y : Integer) : boolean ;
VAR
s : String;
nErr : Integer;
tch : Char;
Done : Boolean;
BEGIN
s := '' ; Done := False ;
REPEAT
tch := ReadKey;
IF tch IN ['0'..'9'] THEN s := s+tch
ELSE IF (Length(s) = 0) AND (tch = '-') THEN s := tch { allow negative }
{ allow backspace ... }
ELSE IF (Length(s) > 0) AND (tch = #8) THEN Delete(s, Length(s), 1)
ELSE IF tch IN [#9, #13] THEN Done := True { end on Tab or Car.Ret }
ELSE Write(#7); { bad character warning }
{ Update the on-screen entry: space-backspace is in case user hit }
{ backspace ... }
gotoxy(x, y); Write(s, ' ', #8);
UNTIL Done;
Val(s, Num, nErr);
GetInteger := (nErr = 0) ; { return true if number is good }
END ;
VAR
Number : LongInt ;
EntryOK : boolean ;
BEGIN
clrscr;
Write('Enter an integer ');
EntryOK := GetInteger(Number,WhereX, WhereY) ;
writeln ;
if EntryOK then writeln('You entered the number ',Number)
else writeln('You goofed !') { this should not happen } ;
END.
I use a similar technique when reading from a file. This allows you to issue
explicit messages about what is wrong, instead of just crashing.
You can modify this idea to meet your specific needs. Good luck.
--- PPoint 2.00
---------------
* Origin: Kingston, Canada (1:249/109.11)
|