Xref: utzoo comp.os.vms:37484 comp.lang.fortran:5085 Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!usc!cs.utexas.edu!uunet!comp.vuw.ac.nz!gp.co.nz!gpwrmdh From: gpwrmdh@gp.co.nz Newsgroups: comp.os.vms,comp.lang.fortran Subject: Re: Checking a string for an integer Message-ID: <1991Apr2.101544.1490@gp.co.nz> Date: 1 Apr 91 22:15:44 GMT References: <1991Apr1.104227.243@biivax.dp.beckman.com> Organization: GP Print Ltd, Wellington, New Zealand Lines: 66 In article <1991Apr1.104227.243@biivax.dp.beckman.com>, dsroberts@biivax.dp.beckman.com writes: > I would like to take a 3 byte string and find out if it contains only ascii > digits for number or if letters were entered. On VMS, the DCL command would be > > result = f$integer (string) > > which would put the numeric equivalent in result, or return an error. I don't > find an obvious way to do this in FORTRAN. Any suggestions? I am not averse > to a VMS system service, btw. Here is a simple example program: program test implicit none character*3 in integer out, status write (*, fmt='(''$number: '')') read (*, fmt='(a3)') in read (unit=in, fmt='(i3)', iostat=status) out if (status .eq. 0) then type *, 'Converted ok' type *, 'Number = ', out type *, 'Returned status = ', status if (out .gt. 254) type *, 'Number too big' else type *, 'Error converting number' type *, 'Status = ', status end if end Basically, you use the READ statement to convert the string to internal format, and check the returned status - success returns a 0. You will probably want more error checking than I have put in the above. The error code returned for invalid numbers is code 64, meaning "input conversion error". > > And, just in case I am missing some better alternative, what I am trying to > accomplish is to find out if this 3 byte string contains a number in a valid > range (in this case 1-254) or not. The only requirement is that I start with a > 3-byte string. Pointers to the appropriate pages in the VMS FORTRAN manuals > would be appreciated :-) Section 4.2.3.2 of the Fortran User's manual (V5.0) has a little about internal i/o, and also section 7.2.4 of the Fortran Reference Manual. > -- > --------------------------------------------------------------------------- > Don Roberts Internet: dsroberts@beckman.com > Beckman Instruments, Inc. Yellnet: 714/961-3029 > 2500 Harbor Bl. Mailstop X-12 FAX: 714/961-3351 > Fullerton, CA 92634 Disclaimer: Always > --------------------------------------------------------------------------- -- ---------------------------------------------------------------------------- Martin D. Hunt GP Print Limited USEnet address : martinh@gp.co.nz Wellington PSI address : PSI%0530147000028::martinh New Zealand Phone : +64 4 4965790 -----------------------------------------------------------------------------