how to get a double from the command line

D

Dawn Minnis

hey guys

i have char *argv[] to receive command line arguements.
I can get characters out no problem
I can get integers parsed out no problem
But how do I get double values.

eg if my program is called test and I run it as

test n t 3 4 2.4 3.55

how do I store the value 2.4 into a variable "double alpha", and the value
3.55 into a variable "double beta"?

Bearing in mind the doubles could be any number of decimal places, I need
to know how to have a general "loop" (I suppose) that will go through and
pick me out the doubles.

You can assume that the values may change in length but that the number of
arguments will always be the same and there will always be only two doubles
at the end to have to store.

Your help on this is greatly appreciated.

Be gentle - beginner/newbie/wet behind the ears etc...

Regards
Dawn
 
W

Walter Roberson

:i have char *argv[] to receive command line arguements.
:I can get characters out no problem
:I can get integers parsed out no problem
:But how do I get double values.

strtod / atod / strtold / atold

Or of course sscanf
 
M

Michael Mair

Dawn said:
hey guys

i have char *argv[] to receive command line arguements.
I can get characters out no problem
I can get integers parsed out no problem
But how do I get double values.

eg if my program is called test and I run it as

test n t 3 4 2.4 3.55

how do I store the value 2.4 into a variable "double alpha", and the value
3.55 into a variable "double beta"?

Bearing in mind the doubles could be any number of decimal places, I need
to know how to have a general "loop" (I suppose) that will go through and
pick me out the doubles.

You can assume that the values may change in length but that the number of
arguments will always be the same and there will always be only two doubles
at the end to have to store.

So, we know:
If argc>=3, then argv[argc-2] and argv[argc-1] contain doubles.

Retrieving doubles from strings:
mydbl = strtod(argv[argc-2], NULL);
or
mydbl = strtod(argv[argc-2], &charptr);
or
ret = sscanf(argv[argc-2], "%lf", &mydbl);

strtod(): If charptr==argv[argc-2] or mydbl=0.0/+-HUGEVAL and ERANGE
is set, errors occurred.
sscanf(): If ret!=1, errors occurred.


HTH
Michael
 
D

Dawn Minnis

Oh

Can you use atod just by accessing the first element of the array of chars.

eg double alpha
alpha = atod(argv[5]);


for example?
 
W

Walter Roberson

:Can you use atod just by accessing the first element of the array of chars.

:eg double alpha
:alpha = atod(argv[5]);

:for example?

argv[5] is a pointer so the question doesn't actually arise.
But Yes, if you had, for example,

char foo[] = "3.14159";
double alpha = atod(foo);

then there is no problem: the array reference will be silently converted
into a pointer to the first element.
 
C

CBFalconer

Dawn said:
.... snip ...

eg if my program is called test and I run it as

test n t 3 4 2.4 3.55

how do I store the value 2.4 into a variable "double alpha", and
the value 3.55 into a variable "double beta"?

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

int main(int argc, char* *argv) {
double alpha, beta;
char *err;

alpha = beta = 0.0;
if (argc < 5) puts("No alpha value");
else {
alpha = strtod(argv[5], &err);
if (err == argv[5]) puts("Bad argv[5]");
else if (*err) puts("Extra junk in argv[5]");
}
if (argc < 6) puts("No beta value");
else {
beta = strtod(argv[6], &err);
if (err == argv[6]) puts("Bad argv[6]");
else if (*err) puts("Extra junk in argv[6]");
}
printf("alpha = %f beta = %f\n", alpha, beta);
return 0;
} /* untested */

(There are other possible errors, not checked above)
 
R

Richard Bos

[ Please do not top-post. Corrected. ]
strtod / atod / strtold / atold

Or of course sscanf

Can you use atod just by accessing the first element of the array of chars.

eg double alpha
alpha = atod(argv[5]);

No, you can't, because there is no atod() in C. There's only atof(). And
in any case, using ato*() isn't recommendable, because they cause
undefined behaviour when they encounter a value they can't represent
(e.g., on overflow). Use strto*() instead.
Apart from this, you're not accessing just the first element, you're
passing a pointer to it. Through this pointer, strto*() (as well as any
other function to which you pass it) can read all of the string. So,
while your description is not quite correct, yes, you do convert the
fifth command line parameter to double by using

alpha=strtod(argv[5], 0);

or if you want to do a bit of error checking as well, something like

char *endptr;

if (argc>5) {
alpha=strtod(argv[5], &endptr);
if (*endptr) report_correct_usage_and_exit();
}

Richard
 
F

Flash Gordon

Walter said:
:i have char *argv[] to receive command line arguements.
:I can get characters out no problem
:I can get integers parsed out no problem
:But how do I get double values.

strtod / atod / strtold / atold

atod and atold are not standard functions. strtold was introduced in C99
and so may well not be available.
Or of course sscanf

Yes.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,769
Messages
2,569,580
Members
45,053
Latest member
BrodieSola

Latest Threads

Top