Re: code question
- From: Barry Schwarz <schwarzb@xxxxxxxx>
- Date: Wed, 24 Sep 2008 19:17:46 -0700
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
.
- Follow-Ups:
- Re: code question
- From: CBFalconer
- Re: code question
- References:
- Re: code question
- From: lovecreatesbea...@xxxxxxxxx
- Re: code question
- From: Richard Heathfield
- Re: code question
- From: lovecreatesbea...@xxxxxxxxx
- Re: code question
- From: Barry Schwarz
- Re: code question
- From: lovecreatesbea...@xxxxxxxxx
- Re: code question
- From: lovecreatesbea...@xxxxxxxxx
- Re: code question
- From: Barry Schwarz
- Re: code question
- From: lovecreatesbeauty@xxxxxxxxx
- Re: code question
- Prev by Date: Re: question
- Next by Date: Re: Any way to take a word as input from stdin ?
- Previous by thread: Re: code question
- Next by thread: Re: code question
- Index(es):
Relevant Pages
|