#1
  1. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13

    What's wrong with this simple IF statement?


    Sorry for the potentially stupid question, but I couldn't locate this basic info on the net.


    A couple paremeters are passed to a program on command line. The second paremeter is in this case OH

    Here's the code...

    Code:
    printf (argv[2]);
    
    if (argv[2]=="OH") {
    	printf ("\n\ndetected OH.  cool\n\n");
    	exit(0);
    } else {
    	printf ("\n\ndidn't see OH.  crap\n\n");
    	exit(0);
    }

    The program prints out OH to the screen, then says "didn't see OH. crap".

    How am I mucking up this if statement?
  2. #2
  3. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    You can't compare strings with == in C. You have to use strcmp() for this.
    Code:
    #include <string.h>
    
    printf (argv[2]);
    
    if (strcmp(argv[2],"OH") == 0) {
    	printf ("\n\ndetected OH.  cool\n\n");
    	exit(0);
    } else {
    	printf ("\n\ndidn't see OH.  crap\n\n");
    	exit(0);
    }
    Hope this helps!
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2002
    Posts
    320
    Rep Power
    13
    Cool, strcmp() works good..

    Is there a way to eliminate the if/else and just make this a if ( doesn't match arguemtns) { exit progrm } ?

    I tried changing the ==0 to !=0 but then neither of the arguments were recognized (even though they were correct).
  6. #4
  7. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    Change || (or) to && (and) and you should be good to go.
    Code:
    if ((strcmp(argv[2],"OH") != 0) && (strcmp(argv[2],"IH") != 0))  {
    	printf ("\nIncorrect location code.   Valid options: O, OH, IH, OL, or EX\n\n");
    	exit(0);
    }
    Of course, you probably want to add more && (strcmp(....) for the rest of the options EX, OL and O.
  8. #5
  9. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Sep 2002
    Location
    South Carolina
    Posts
    0
    Rep Power
    0

    I hope this helps


    Code:
    #include <string.h>
    
    int main(int argc, char **argv){
    
       if(argc != 3){ //1 || argc > 2){
          printf("\nWARNING: Incorrect usage!  Proper usage...\n");
          exit(1);
       }
    
       printf (argv[2]);
    
       if (!(strcmp(argv[2],"OH") == 0)){
          printf ("\n\ndidn't see OH.  crap\n\n");
          exit(1);
       }
    
       printf ("\n\ndetected OH.  cool\n\n");
    }
  10. #6
  11. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    To avoid confusion here, C3PO's 2nd post on this thread was asking what was wrong with this code:
    Code:
    if (strcmp(argv[2],"OH") != 0 || strcmp(argv[2],"IH") != 0)  {
    	printf ("\nIncorrect location code.   Valid options: O, OH, IH, OL, or EX\n\n");
    	exit(0);
    }
    Unfortunately he edited the post (and removed all the code) right when I posted my reply. :)
  12. #7
  13. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2002
    Posts
    36
    Rep Power
    13
    Uhm if you put argv[2] wouldn't that just be comparing the character in argv index 2? (so wouldnt it be only 1 char)
  14. #8
  15. Banned ;)
    Devshed Supreme Being (6500+ posts)

    Join Date
    Nov 2001
    Location
    Woodland Hills, Los Angeles County, California, USA
    Posts
    9,641
    Rep Power
    4247
    Nope, argv is declared as char ** (or char *argv[] in some cases), which means it's a pointer to pointer to char (or pointer to array of chars if you like). So argv[2] points to the third char array (not 2nd char array, since arrays are zero based in C. However, this array is the second argument passed to the program, since argv[0] is the program name itself). If you want the 2nd char of the 3rd char array, then you could perhaps try argv[2][1].

    Hope this helps!

IMN logo majestic logo threadwatch logo seochat tools logo