fs@uwasa.fi (Filip Sawicki LAKE) (02/27/90)
In article <1458@amethyst.math.arizona.edu> raw@math.arizona.edu (Rich Walters) writes: >In article <8146@hubcap.clemson.edu> kaires@hubcap.clemson.edu (Robert G Kaires) writes: >> this function has the syntax: >> double strtod(const char *s, char **endptr); >> [deleted] >A better way might be [deleted] > char string[30]; > char *ptr; > double ans; > extern double strtod(); > > while(1) { > gets(string); > if (*string == 'q') break; > ans=strtod(string,&ptr); > if ( ( string+strlen(string) ) != ptr ) /* <--- warning here */ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ WRONG !!! > printf("format error\n"); > else > printf("You typed the number: %f\n",ans); > } >} > > >This works. > Richard Walter That does not compute - especially when the string is empty (*string=='\0'). According to the SUN manual, strtod returns ptr==string if string is completely inconvertible, otherwise ptr points to the first wrong character. So, checking should be as follows: if (ptr==string || *ptr) printf("format error\n"); else /* *ptr=='\0' - string not empty and all chars were transformed */ printf( ... ); fi.
scm@dlcq15.datlog.co.uk (Steve Mawer) (03/07/90)
In article <1990Feb27.110204.1415@uwasa.fi> fs@uwasa.fi (Filip Sawicki LAKE) writes: > >According to the SUN manual, strtod returns ptr==string if string is >completely inconvertible, otherwise ptr points to the first wrong character. Beware! I used strtol() on 2.2.1 RT AIX and found that the returned pointer from the string "\n\0" pointed to the *null*, not the newline. The code affected had been ported to a number of environments and AIX was the only one thus afflicted. -- Steve C. Mawer <scm@datlog.co.uk> or < {backbone}!ukc!datlog!scm > Voice: +44 1 863 0383 (x2153)