Code: What am I doing wrong? 
Author Message
 Code: What am I doing wrong?

I am trying to test a command line argument, unfortunately I always
seem to get the false leg. I've tried numerous sytanx', but can't seem
to come up with anything. Could someone please help me out on this?

  main (int agrc, char *argv[])
  {
    if ("argv[1]" == "-al")
      system("dir /m /k");
    else
      system("dir /w /m /k");
  }

If the user has a parameter of  -al  then it should execute the true
leg.

Thanks for the  help.

Canada Bass

"Remove the no_spam to reply."



Wed, 22 Dec 1999 03:00:00 GMT  
 Code: What am I doing wrong?

Sorry for the add on, but I forgot to ask. How do you pass a command
line parameter to a system call? In that I am unsucessful also.

Thanks again.
Canada Bass



Quote:
>I am trying to test a command line argument, unfortunately I always
>seem to get the false leg. I've tried numerous sytanx', but can't seem
>to come up with anything. Could someone please help me out on this?

>  main (int agrc, char *argv[])
>  {
>    if ("argv[1]" == "-al")
>      system("dir /m /k");
>    else
>      system("dir /w /m /k");
>  }

>If the user has a parameter of  -al  then it should execute the true
>leg.

>Thanks for the  help.

>Canada Bass

>"Remove the no_spam to reply."



Wed, 22 Dec 1999 03:00:00 GMT  
 Code: What am I doing wrong?

Quote:

> I am trying to test a command line argument, unfortunately I always
> seem to get the false leg. I've tried numerous sytanx', but can't seem
> to come up with anything. Could someone please help me out on this?
>     if ("argv[1]" == "-al")

At first, you can't use "" around a symbol name. Then you can't compare
strings like you do. In c a string is implemented as an array of chars.
If you use the == operator like this you do in fact try compare the
memory location of argv[1] and "-al".

The correct thing to do would be like this:

if(!strcmp(argv[1],"-al"))

where strcmp returns 0 if the strings are equal.




Wed, 22 Dec 1999 03:00:00 GMT  
 Code: What am I doing wrong?


Quote:
> I am trying to test a command line argument, unfortunately I always
> seem to get the false leg. I've tried numerous sytanx', but can't seem
> to come up with anything.
>   main (int agrc, char *argv[])
>   {
>     if ("argv[1]" == "-al")

You cannot compare strings directly in C. You must use a function:

if(!strcmp(argv[1], "-al"))

also, you don't need to put argv[1] inside quotation marks, it is already a
pointer to a string (or NULL).

Quote:
>       system("dir /m /k");
>     else
>       system("dir /w /m /k");
>   }



Wed, 22 Dec 1999 03:00:00 GMT  
 Code: What am I doing wrong?

|>   main (int agrc, char *argv[])
|>   {
|>     if ("argv[1]" == "-al")
           a) ^^^^  b)^^

a) Why are you quoting the array reference? argv[1] is the first
argument to the program, while "argv[1]" is a string literal.

b) You'ree using numeric comparison ("==")  on strings. Look up
strcmp() in your compiler manual or online help.

--
*    "We all agree on the necessity of compromise.  We just can't agree on
*     when it's necessary to compromise."



Fri, 24 Dec 1999 03:00:00 GMT  
 
 [ 5 post ] 

 Relevant Pages 

1. What am i doing wrong in this code?

2. what am i doing wrong?

3. what am I doing wrong here?

4. What am I doing wrong?

5. What am I doing wrong?

6. WHAT AM I DOING WRONG?

7. what am i doing wrong!

8. What am I doing wrong (part 2)

9. What am i doing wrong?

10. What am I doing wrong here

11. Help I cant C what I am doing wrong

12. What am I doing Wrong?

 

 
Powered by phpBB® Forum Software