468,504 Members | 1,966 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

Post your question to a community of 468,504 developers. It's quick & easy.

strtod( )

Since I've been told that char *argv[] or char **argv[] must be the
second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {
if (argc != 3) {

fprintf(stderr,"usage error");
}
strtod(argv[1],(char**);
strtod(argv[2],(char**);

printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);
}

This is one of many tried of strtod( ) I've tried on my own.

Bill
Mar 25 '08 #1
8 3157
Bill Cunningham wrote:
Since I've been told that char *argv[] or char **argv[] must be the
second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {
if (argc != 3) {

fprintf(stderr,"usage error");
}
strtod(argv[1],(char**);
How could this compile?

strtod() returns a value, which you are discarding.

The simplest use of strtod() has NULL for the second parameter, for example:

double d = strtod( argv[1], NULL );

You should clear errno and check its value for zero after the call to
make sure the conversion was successful.

--
Ian Collins.
Mar 25 '08 #2
How could this compile?

strtod() returns a value, which you are discarding.

The simplest use of strtod() has NULL for the second parameter, for
example:

double d = strtod( argv[1], NULL );
I read or thought I read the second argument was a pointer.
You should clear errno and check its value for zero after the call to
make sure the conversion was successful.

--
Ian Collins.

Mar 25 '08 #3
So I guess it's char *argv[] and can't be double *argv[] if doubles
are wanted then.

Mar 25 '08 #4
no****@nspam.com wrote:
So I guess it's char *argv[] and can't be double *argv[] if doubles
are wanted then.
What can? What or who are you replying to?

--
Ian Collins.
Mar 25 '08 #5
On Tue, 25 Mar 2008 01:06:49 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
Since I've been told that char *argv[] or char **argv[] must be the
I doubt if anyone has suggested the second form. If they have, you
have just learned an important Usenet lesson: some advice is wrong.
More likely, however, is the lesson that in programming you need to
pay more attention to the details. The second form of this parameter
is char** argv. The difference is not irrelevant...
>second parameter to main's command line structure I have turned to strtod( )
but can't get it to work so far. This function is probably used alot but I
obviously am not using it right.

#include <stdio.h>

int main(int argc, char **argv) {
But apparently you knew the correct form. So maybe your opening
sentence was sucker bait and you hooked me.
if (argc != 3) {

fprintf(stderr,"usage error");
You have been here for a while. How many times have you seen messages
discussing the potential problems caused by not including a \n at the
end of the string to be displayed?
}
strtod(argv[1],(char**);
You might want to spend some time learning to balance your
parentheses.

You might also want to practice indenting consistently. Code that
easier to read is easier to debug.

When you look up a function in your reference, it should also tell you
which headers you need to include to use that function. That
information is not superfluous.
strtod(argv[2],(char**);

printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);
This is just mind boggling. Do you really think you can just ignore
the function prototypes? Do you really think that placing arguments
adjacent to each other with no intervening punctuation will magically
convert and concatenate values? Do you really think two conversion
specifications are adequate for printing at least three variable
values?
>}
Where is the int that main is supposed to return?
>
This is one of many tried of strtod( ) I've tried on my own.
Forget strtod. Try to get main correct first.

Remove del for email
Mar 25 '08 #6
Barry Schwarz wrote:
On Tue, 25 Mar 2008 01:06:49 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
[blither]
Forget strtod. Try to get main correct first.

I recommend not wasting your time. He's supposedly been learning C
since 2004. Nothing you say will make the least impression. Most likely
he will just be moving on to some new problem he doesn't understand.

Until Cunningham agrees to learn C by defined rules, including working
one problem at a time through to success, and looking up functions in
manuals or online, people should just shut him out.

Yes, I'm an ol' meany-head because I have no sympathy for his supposed
learning disability. Well, if that prevents him from learning C at all,
which it seems to, then he should find some more productive use of his
time.


Brian
Mar 25 '08 #7
[snip]
>>#include <stdio.h>

int main(int argc, char **argv) {

But apparently you knew the correct form. So maybe your opening
sentence was sucker bait and you hooked me.
The above statement you refer to is a misprint.

Bill
Mar 25 '08 #8
On Mon, 24 Mar 2008 22:25:15 -0700, Barry Schwarz <sc******@dqel.com>
wrote:
On Tue, 25 Mar 2008 01:06:49 GMT, "Bill Cunningham" <no****@nspam.com>
wrote:
<snip other points>
printf("%f"argcv[1],"divided by ","%f",argv[2]," is
",argv[1]/argv[2]);

This is just mind boggling. Do you really think you can just ignore
the function prototypes?
If the juxtaposition worked (see next) this wouldn't violate the
prototype -- only the semantics for printf's varargs, which aren't in
the prototype. (Since *printf/scanf are standard, the compiler can
know about them and e.g. gcc does, but not from the prototype.)
Do you really think that placing arguments
adjacent to each other with no intervening punctuation will magically
convert and concatenate values?
It does in awk, whose surface syntax is enough like C (at least the C
when it was created, K&R1) that on a cloudy night, in a fog, with your
eyes closed, you might confuse them. <G>
Do you really think two conversion
specifications are adequate for printing at least three variable
values?
If the (inconsistent and probably unintended, and misspelled)
juxtaposition had in fact done one conversion, you would have two
conversion specifiers and two remaining data items -- if there were
some way for printf distinguish which was intended to be which, which
there isn't. (Plus the fact, already discussed, that argv[i] has to be
a string and can't be a float or double.)

- formerly david.thompson1 || achar(64) || worldnet.att.net
Apr 7 '08 #9

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

3 posts views Thread by Leslaw Bieniasz | last post: by
1 post views Thread by Mathieu Malaterre | last post: by
21 posts views Thread by Marky C | last post: by
9 posts views Thread by Adam Warner | last post: by
4 posts views Thread by Gary Wessle | last post: by
18 posts views Thread by coder | last post: by
reply views Thread by NPC403 | last post: by
reply views Thread by fmendoza | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.