home *** CD-ROM | disk | FTP | other *** search
- Path: sparky!uunet!spool.mu.edu!mixcom.com!ddxxdd
- From: ddxxdd@mixcom.com (Jeff Ding)
- Newsgroups: comp.lang.pascal
- Subject: Re: want to help me make my 'value' routine crash-proof?
- Message-ID: <1992Aug25.214151.3208@mixcom.com>
- Date: 25 Aug 92 21:41:51 GMT
- Article-I.D.: mixcom.1992Aug25.214151.3208
- References: <1992Aug25.150516.17385@javelin.sim.es.com>
- Organization: Milwaukee Internet Xchange BBS, Milwaukee, WI U.S.A.
- Lines: 61
-
- In article <1992Aug25.150516.17385@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
- >
- >function value(I:string):word;
- >var n:real; n1:integer;
- > begin
- > val(i,n,n1);
- > if n1<>0 then begin
- > i:=copy(i,1,n1-1);
- > val(i,n,n1);
- > end;
- > value:=n;
- > if i='' then value:=0;
- > end;
- >
- >of course, when i pass a loaded string such as '99999' the routine crashes.
- >maxing out at 65535 is desired.
- >
- >so what's the quick and dirty?
- >
-
- Here is my example String-To-Word conversion program. It is quick, simple
- and even converts negative numbers appropriately. Any error will result
- in the word equal to 0. You can add an error flag very easily.
-
-
- program TestValue;
-
-
- var S : string;
- V : word;
-
-
- function StrToWord(S: string): word;
-
- var N : word;
- Err : integer;
-
- begin
- Val(S, N, Err);
- if Err > 0 then S:= Copy(S, 1, Err - 1);
- Val(S, N, Err);
- if (Length(S) < 7) and (S < '65536') then
- StrToWord:= N else StrToWord:= 0
- end; {StrToWord}
-
-
- begin
- write('Enter a number: ');
- readln(S);
- V:= StrToWord(S);
- writeln('The ''WORD'' value is: ',V);
- end.
-
-
- >--
- >\clinkenpeel: aberrant analytical cynical agnostic idealist. i exclusively /
- >/represent myself. i do not offend anyone; however, some morons choose to \
- >\offend themselves. judge only those who you would have judge yourself. i /
- >/don't practice what i preach - i'm not the type of person i'm preaching to.\
-
- jeff.ding@mixcom.com
-