Thread: Why dosen't this work

  1. #1
    Registered User
    Join Date
    Aug 2002
    Posts
    109

    Why dosen't this work

    Can some please tell me why this doesn't work because I just can't fingure it out.
    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    int main(int argc, char *argv[])
    {
    if ("help" == argv[1])
    {
    system("help.exe");
    }
    printf("%s\n",argv[1]);
    }

  2. #2
    Registered User JoshG's Avatar
    Join Date
    Mar 2002
    Posts
    326
    It does not work, or it won't compile? Be sure to check argc first.

    Also return 0;
    Last edited by JoshG; 08-16-2002 at 04:50 PM.

  3. #3
    Green Member Cshot's Avatar
    Join Date
    Jun 2002
    Posts
    892
    Logically, it should be:
    if(argv[1] == "help")
    However, you can't compare strings like that. Use strcmp().
    Try not.
    Do or do not.
    There is no try.

    - Master Yoda

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Casual Visitor
    Join Date
    Oct 2001
    Posts
    350
    Try this, though I dunno what the results will be or if it will work.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int main(int argc, char *argv[])
    {
         if(argc == 2)
         {
             if((strcmp(argv[1], "help")) == 0)
                  printf("help was entered as an argument\n"); 
             else
                  printf("%s was entered as an argument\n", argv[1]);
          }
         
         else
                 printf("no argument enetered, or more than one argument entered.\n");
    
          return 0;
    }
    I haven't used a compiler in ages, so please be gentle as I try to reacclimate myself. :P

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  2. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  3. Problems in getting OpenGL to work
    By zonf in forum C Programming
    Replies: 5
    Last Post: 02-13-2006, 04:48 AM
  4. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM
  5. sigh... why dosent this work?
    By jon_nc17 in forum C++ Programming
    Replies: 3
    Last Post: 05-20-2002, 08:30 PM