Re: code question



On Tue, 23 Sep 2008 19:37:50 -0700 (PDT),
"lovecreatesbeauty@xxxxxxxxx" <lovecreatesbeauty@xxxxxxxxx> wrote:

On Sep 24, 9:50 am, Barry Schwarz <schwa...@xxxxxxxx> wrote:
On Mon, 22 Sep 2008 22:38:35 -0700 (PDT),
"lovecreatesbea...@xxxxxxxxx" <lovecreatesbea...@xxxxxxxxx> wrote:

#include <stdlib.h>
#include <stdio.h>
#include <errno.h>
#include <string.h>

int main (int argc, char *argv[])
{
double d;
char **a = argv, *e;

while (*++a){
errno = 0;
d = strtod(*a, &e);
if (errno){
perror(*a);
continue;
}
if (d == 0 && *a == e){

The expression d==0 is still unnecessary.


The documents (the Standard, manpages) say:

If no conversion is performed, zero is returned and the value of
nptr
is stored in the location referenced by endptr.

So I checked those two.

If *a ==e, d must be 0. If d !=0, then *a == e must be false also.
The fact that zero is returned is just a simple way to guarantee that
the behavior is not undefined (as happens with ato.. functions).

Put another way, if d==0, you still must check *a and e. If d == 0 is
falser, then *a == e is also guaranteed to be false. The check on d
provides no additional information.



fprintf(stderr, "%s: Cant be converted\n",
*a);
continue;
}
while (*e)
if (!isspace(*e++))
break;
if (*e){
fprintf(stderr, "%s: Invalid format\n", *a);
continue;
}

Why do you consider input of 12xy to be "better" (or even different)
than input of xy12?


But my code issues error messages on both inputs already.

strtod can accept "12xyz" and "12 xyz" and return (double)12. Can I
also accept these inputs in some of my code?

You can do whatever you want with 12xy but, in my opinion, I don't
see why you would want to accept it as 12.0. A malformed input is
malformed whether the error is on the first or the last character or
any in between.



fprintf(stdout, "%f, %s\n\n", d, *a);
}
return EXIT_SUCCESS;
}


--
Remove del for email
.



Relevant Pages

  • Re: Predicted Numbers for the month of June 2006
    ... Unless you can show us another system that produce a guarantee ... whatever lucky numbers that can produce a sure win every week. ... There is no guarantee of winning in any form of gambling. ... 1678 - Zero ...
    (soc.culture.singapore)
  • Re: int** to void**
    ...  For integer types, memset will set the elements to zero. ... Or you can use memset to zero it, but keep in mind that, though this ... I think the concern here is assign integer 0 to pointer or double in C ... there is no such guarantee. ...
    (comp.lang.c)
  • Re: Initialization of struct to zero with a cast
    ... use calloc instead? ... struct to zero" ... But unless your struct T consists entirely of integer members ... (The guarantee that all-bita-zero is a representation ...
    (comp.lang.c)
  • Re: Predicted Numbers for the month of June 2006
    ... Although the return is 10% in one one month, but you cannot guarantee that your numbers produced the same return for for the following months. ... There is no guarantee of winning in any form of gambling. ... All we need to do is to pick a few lucky to you and bet on it. ... 1678 - Zero ...
    (soc.culture.singapore)
  • Re: Templates, again
    ... Jonathan Turkanis wrote: ... > True, you can't guarantee much about error messages, but I think the ...
    (comp.lang.cpp)