home *** CD-ROM | disk | FTP | other *** search
/ NetNews Usenet Archive 1992 #19 / NN_1992_19.iso / spool / comp / lang / pascal / 5052 < prev    next >
Encoding:
Internet Message Format  |  1992-08-25  |  1.8 KB

  1. Path: sparky!uunet!spool.mu.edu!mixcom.com!ddxxdd
  2. From: ddxxdd@mixcom.com (Jeff Ding)
  3. Newsgroups: comp.lang.pascal
  4. Subject: Re: want to help me make my 'value' routine crash-proof?
  5. Message-ID: <1992Aug25.214151.3208@mixcom.com>
  6. Date: 25 Aug 92 21:41:51 GMT
  7. Article-I.D.: mixcom.1992Aug25.214151.3208
  8. References: <1992Aug25.150516.17385@javelin.sim.es.com>
  9. Organization: Milwaukee Internet Xchange BBS, Milwaukee, WI U.S.A.
  10. Lines: 61
  11.  
  12. In article <1992Aug25.150516.17385@javelin.sim.es.com> tpehrson@javelin.sim.es.com writes:
  13. >
  14. >function value(I:string):word;
  15. >var n:real; n1:integer;
  16. > begin
  17. > val(i,n,n1);
  18. > if n1<>0 then begin
  19. >  i:=copy(i,1,n1-1);
  20. >  val(i,n,n1);
  21. > end;
  22. > value:=n;
  23. > if i='' then value:=0;
  24. > end;
  25. >
  26. >of course, when i pass a loaded string such as '99999' the routine crashes.
  27. >maxing out at 65535 is desired.
  28. >
  29. >so what's the quick and dirty?
  30. >
  31.  
  32. Here is my example String-To-Word conversion program.  It is quick, simple
  33. and even converts negative numbers appropriately.  Any error will result
  34. in the word equal to 0.  You can add an error flag very easily.
  35.  
  36.  
  37. program TestValue;
  38.  
  39.  
  40. var  S : string;
  41.      V : word;
  42.  
  43.  
  44. function StrToWord(S: string): word;
  45.  
  46.   var  N   : word;
  47.        Err : integer;
  48.  
  49.   begin
  50.     Val(S, N, Err);
  51.     if Err > 0 then S:= Copy(S, 1, Err - 1);
  52.     Val(S, N, Err);
  53.     if (Length(S) < 7) and (S < '65536') then
  54.       StrToWord:= N else StrToWord:= 0
  55.   end;  {StrToWord}
  56.  
  57.  
  58. begin
  59.   write('Enter a number: ');
  60.   readln(S);
  61.   V:= StrToWord(S);
  62.   writeln('The ''WORD'' value is: ',V);
  63. end.
  64.  
  65.  
  66. >-- 
  67. >\clinkenpeel: aberrant analytical cynical agnostic idealist.  i exclusively /
  68. >/represent myself.  i do not offend anyone; however, some morons choose to  \
  69. >\offend themselves.  judge only those who you would have judge yourself.  i /
  70. >/don't practice what i preach - i'm not the type of person i'm preaching to.\
  71.  
  72. jeff.ding@mixcom.com
  73.