Problem with argv

I'm running a linux... actually an android with Terminal IDE installed(amazing app) and I created this program with command line arguments...
I run it no errors, but I want them to be able to add a -h option
I have the =="-h" part but still no luck any help would be great...
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
#include <stdio.h>
int main(int argc, const char *argv[])
{
if (argc==1)
{
printf("Please add an argument\n");
return 1;
}
if(argc==2 && argv[1]=="-h")
{
printf(" Usage:\n");
printf("./hello [-h] NAME\n");
printf("-h displays help\n");
}
if(argc==2 && (argv[1]!="-h"))
{
printf("Hello, ");
printf(argv[1]);
printf("! Thank You!\n");
}
return 0;
} 
What is the output of the above program if you give -h as the parameter?
argv[1]=="-h"

That's not how you compare C-style char* strings. Use strcmp.
Last edited on
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
#include <stdio.h>
#include <string.h>

int main(int argc, const char *argv[])
{
   if ( argc == 1 )
   {
      printf( "Please add an argument\n" );
      return 1;
   }
   else if ( argc == 2 )
   {
      if ( stdcmp( argv[1], "-h" ) == 0 )
      {
         printf( " Usage:\n" );
         printf( "./hello [-h] NAME\n" );
         printf("-h displays help\n");
      }
      else 
      {
         printf( "Hello, " );
         printf( argv[1] );
         printf("! Thank You!\n");
      }
   }

   return 0;
} 
With single char switches like -h it is also quite common to use operator[] and a switch statement, like below, rather than strcmp. (The code is also tweaked so it treats arg counts other than 1 or 2, and also switches other than -h, as an error.)

And note that that, for historic reasons, the standard from of main uses char* argv[] rather than const char* argv[].

Andy

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
#include <stdio.h>

void usage()
{
    const char msg[] =
        "Usage:\n"
        "./hello NAME\n"
        "  displays NAME\n"
        "./hello -h\n"
        "  displays help\n";
    printf("%s", msg);
}

int main(int argc, char* argv[])
{
    if (argc!=2)
    {
        printf("Please provide a single argument\n\n");
        usage();
        return 1;
    }

    if (argv[1][0]=='-')
    {
        switch (argv[1][1])
        {
        case 'h':
        case 'H':
            usage();
            break;
        default:
            printf("invalid switch : %s\n\n", argv[1]);
            usage();
            return 1;
        }
    }
    else
    {
        printf("Hello, %s! Thank You!\n", argv[1]);
    }

    return 0;
}
Last edited on
I highly recommend using getopt (3C) which is the best portable way to handle command line arguments IMO.
I like boost.program_options more than the posix getopt
http://www.boost.org/doc/libs/release/doc/html/program_options.html
Thank you for all your help... It works perfectly but question...
Line #23 & #25 why a 2 dimmensional string...
Hey vlad I would try your code... but I have to get a copy of string.h, iostream, etc. Does anyone know where I can get this? My compiler didn't build right
Line #23 & #25 why a 2 dimmensional string...

There's no such thing as a "2 dimensional string". argv is an array of strings. The first member, argv[0], is the name of the application. Each subsequent member of the array is one word from the command line.

I have to get a copy of string.h, iostream, etc. Does anyone know where I can get this?

They're part of the C++ standard library, and should be supplied with any compiler.
Last edited on
@timprograms

Hey vlad I would try your code... but I have to get a copy of string.h, iostream, etc. Does anyone know where I can get this? My compiler didn't build right


First of all you should decide whether you will write the program in C or C++. If the program will be written in C you need to include header <string.h>. If the program will be written in C++ you need to include header <cstring>. These headers are part of the standard libraries of the compiler. So it will be simply enough to specify them in your program.
Last edited on
Thank you for you help it worked with the 2d array
Topic archived. No new replies allowed.