Question:
How to put numbers on the command line, C programming?
Mc Mister
2012-01-26 20:05:27 UTC
Ok so this may seem like a really simple thing but for some reason I can not get it to work. The program works just fine, but I can not get the program to run without pressing enter after typing in the program name. For the program it reads in 4 values, these four values are to be read from the command line; the user should not be prompted for the
input. Once compiled (say into a.out) it should be executable from the command line as follows:
a.out 1.5 2.0 10.25 20. I do not know how to do it. Here is the code:

/*Name: Anonymous
* Date: 01/25/2012
* This program takes points and tells you the
* distance by typing the valuses into the comand line.*/
#include
#include //needed for the sqrt function

int main(void)
{
double x1,x2,y1,y2,distance; //all our variblles
scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);
// using the distance formula to solve the problem
distance=sqrt(pow((x1-x2),2.0)+pow((y1-y2),2.0));
printf("Distance equals %.4f \n",distance);
return 0;
}
Four answers:
husoski
2012-01-26 20:34:36 UTC
The command line is parsed into tokens and the information is provided to main() as a pair of arguments that you are ignoring. Declare:



int main(int argc, char *argv[])

{

...

}



That is, argv is an array of string pointers. The first string, argv[0] is not really an argument. It is conventionally the name of the program being run. Your example looks like Unix (or Linux), so argv[0] might well point to something like "\home\Anonymous\a.out" if the full path is used, or "a.out" or "./a.out" might be there. I don't know, and the C standard doesn't say that there's even a program name there. If there are arguments, they are pointed to by argv[1], argv[2], ...



The argc argument is the number of entries in argv[], counting the non-argument in argv[0]. So, argc is always positive and 1 more than the number of actual arguments. On Unix systems, argv[argc] exists and is a 0 (NULL) pointer.



Lecture mode off.



So, use either sscanf() or atof() to convert individual argument strings into doubles.



if (argc != 5)

{

printf("Usage: %s x1 y1 x2 y2\n", argv[0]);

return 1;

}



sscanf(argv[1], "%lf", &x1);

sscanf(argv[2], "%lf", &y1);

sscanf(argv[3], "%lf", &x2);

sscanf(argv[4], "%lf", &y2);



Note the test for the correct number of arguments. You don't want to be subscripting argv for arguments that aren't there. The alternative (no worse if you don't look at sscanf() return values) is atof:



x1 = atof(argv[1]);

y1 = atof(argv[2]);



...and so on.



I use sscanf, mostly, and put it in a function like:



int str2dbl(char *str, double *pdbl)

{

char dummy;

int n = sscanf(str, " %lf %c", pdbl, &dummy);

return (n==1);

}



That does the conversion and returns 1 (true) if successful or 0 (false) if the input didn't look like exactly one numeric value, with perhaps some leading or trailing spaces. You probably don't (yet) need all that bulletproofing, but it's good to know that there are easy standard library tools to get solid input parsing and error testing done.



Have fun!
Link H
2012-01-26 20:25:03 UTC
instead of



int main(void) , you must declare the command line inputs as



int main(char ** argv)



Then you can parse the arguments from the input to the program variables.



Unfortunately, the details of parsing are system-dependent, since the OS decides how the variables get passed.



See the source reference for an example from Microsoft.
koltay
2016-10-06 14:55:33 UTC
SWF to Video Scout - Command Line a H - run application in computerized mode /S - set output video width and height in sort of WIDTHxHEIGHT, working example: /H320x240 /F - set output video FPS. working example: /F15 /N - do no longer seize sound
JoelKatz
2012-01-26 20:20:25 UTC
Change 'int main(void)' to 'int main(int argc, char **argv)'. Then instead of 'scanf("%lf%lf%lf%lf", &x1, &y1, &x2, &y2);' use 'x1=atof(argv[1]); y1=atof(argv[2]); x2=atof(argv[3]); y2=atof(argv[4]);`


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...