468,513 Members | 1,712 Online
Bytes | Developer Community
New Post

Home Posts Topics Members FAQ

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

command line argument

25
I made a program to be able to change the value of four variables (will be shown in the program) at the command line arguments.

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

{

double minx = -1.0;
double maxx = 1.0;
double miny = -1.0;
double maxy = 1.0;
int i;

for (i = 1;i < argc;i++)
{
if (strcmp(argv[i],"-range") == 0)
{
minx = atof(argv[++i]);
maxx = atof(argv[++i]);
miny = atof(argv[++i]);
maxy = atof(argv[++i]);
}
}
this code will work after I compile with cc compiler. Let's say the filename is range.c

cc range.c
a.out -range 1.0 2.0 3.0 4.0

But how do I stop the program (show error message) if less than four values entered, for example

cc range.c
a.out -range 1.0 2.0 3.0

Someone help me please
thank you
Sep 12 '06 #1
2 9583
Banfa
9,056 Expert Mod 8TB
Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2.  
  3. ...
  4.  
  5. if (argc < 5)
  6. {
  7.   // may be output an error message here
  8.   exit(EXIT_FAILURE);
  9. }
  10.  
Sep 12 '06 #2
tyreld
144 100+
Expand|Select|Wrap|Line Numbers
  1. #include "stdlib.h"
  2.  
  3. ...
  4.  
  5. if (argc < 5)
  6. {
  7.   // may be output an error message here
  8.   exit(EXIT_FAILURE);
  9. }
  10.  

You still have to take into account that the program name is counted as one of the arguments. So, in this example you would actually be expecting no less than 6 arguments. The first being the program name. The second being the -range option, and the remaining 4 being the values.

Expand|Select|Wrap|Line Numbers
  1. if (argc < 6)
  2. {
  3.   // error message
  4.   exit(1);
  5. }
  6.  
Sep 12 '06 #3

Post your reply

Sign in to post your reply or Sign up for a free account.

Similar topics

7 posts views Thread by Bernd Danberg | last post: by
3 posts views Thread by Shailesh Humbad | last post: by
34 posts views Thread by Roman Mashak | last post: by
6 posts views Thread by evantri | last post: by
6 posts views Thread by =?Utf-8?B?Rm9ycmVzdCBIZWxsZXI=?= | last post: by
2 posts views Thread by =?Utf-8?B?UmFtb24gR2VuZQ==?= | last post: by
By using this site, you agree to our Privacy Policy and Terms of Use.