473,386 Members | 1,846 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,386 software developers and data experts.

how to get a double from the command line

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
Nov 14 '05 #1
7 10002
In article <42*********************@ptn-nntp-reader03.plus.net>,
Dawn Minnis <fi**********@btinternet.com> wrote:
: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
--
Oh, to be a Blobel!
Nov 14 '05 #2
Dawn Minnis wrote:
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
--
E-Mail: Mine is an /at/ gmx /dot/ de address.
Nov 14 '05 #3
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?

strtod / atod / strtold / atold

Or of course sscanf
--
Oh, to be a Blobel!

Nov 14 '05 #4
In article <42*********************@ptn-nntp-reader03.plus.net>,
Dawn Minnis <fi**********@btinternet.com> wrote:
: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.
--
Beware of bugs in the above code; I have only proved it correct,
not tried it. -- Donald Knuth
Nov 14 '05 #5
Dawn Minnis wrote:
.... 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)

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!

Nov 14 '05 #6
"Dawn Minnis" <fi**********@btinternet.com> wrote:

[ 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
Nov 14 '05 #7
Walter Roberson wrote:
In article <42*********************@ptn-nntp-reader03.plus.net>,
Dawn Minnis <fi**********@btinternet.com> wrote:
: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.
--
Flash Gordon
Living in interesting times.
Although my email address says spam, it is real and I read it.
Nov 14 '05 #8

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

6
by: deanfamily | last post by:
I am re-posting my second problem. I have a double-linked list. I need to know if it is possible to remove just one of an item, instead of all that match the given criteria with the remove()...
69
by: fieldfallow | last post by:
Hello all, Before stating my question, I should mention that I'm fairly new to C. Now, I attempted a small demo that prints out the values of C's numeric types, both uninitialised and after...
9
by: Greg Buchholz | last post by:
/* While writing a C++ version of the Mandelbrot benchmark over at the "The Great Computer Language Shootout"... http://shootout.alioth.debian.org/gp4/benchmark.php?test=mandelbrot&lang=all ...
0
by: Dean N. Williams | last post by:
Dear Python and Mac Community, I have just successfully built gcc version 4.1.0 for my Mac OS X 10.4.6. gcc -v Using built-in specs. Target: powerpc-apple-darwin8.6.0 Configured with:...
52
by: lcw1964 | last post by:
Greetings, all, I am trying to port a little bit of math code to gcc, that in the original version used the long double version of several functions (in particular, atanl, fabsl, and expl). I...
17
by: arindam.mukerjee | last post by:
I was running code like: #include <stdio.h> int main() { printf("%f\n", 9/5); return 0; }
3
by: Stef Mientki | last post by:
It looks like sometimes a single backslash is replaced by a double backslash, but sometimes it's not ??? See the error message below, the first backslash is somewhere (not explicitly in my code)...
29
by: Virtual_X | last post by:
As in IEEE754 double consist of sign bit 11 bits for exponent 52 bits for fraction i write this code to print double parts as it explained in ieee754 i want to know if the code contain any...
6
by: =?Utf-8?B?R2Vvcmdl?= | last post by:
Hello, I have some XML that is returned to my application from another vendor that I cannot change before it gets to me. I can only alter it after it gets to my application. That being said, I...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.