Conditional if based on command line arguments problem

This is a discussion on Conditional if based on command line arguments problem within the C Programming forums, part of the General Programming Boards category; Hi, I'm trying to make a program that reads in the command line arguments and depending on what is entered ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    10

    Conditional if based on command line arguments problem

    Hi,

    I'm trying to make a program that reads in the command line arguments and depending on what is entered sets mode to either encrypt or decrypt, the problem is it's only doing what is in the else section of the conditional if which means that the conditions for the if statement are incorrect, yet if I do

    printf("You have specified that you wish to \"%s\"option",argv[1])

    it prints quite clearly that the command entered was for example -e or -d and the check to make sure commands were passed confirms that the appropriate number of commands were passed.

    PHP Code:
    int main(int argcchar *argv[]) 
    PHP Code:
    if (argv[1]=="-d") {
        
    char mode[6]="decrypt";
    } else if (
    argv[1]=="e") {
        
    char mode[6]="encrypt";
    } else {
        
    printf("No mode selected");
    }
    printf("%s",mode);
    printf("You have specified that you wish to \"%s\"option",argv[1]); 
    Any help greated appreciated
    thanks

  2. #2
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    what you are doing wrong is string comparison

    use stcmp

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You don't compare strings with ==. Use strcmp or similar instead.

  4. #4
    Registered User
    Join Date
    Nov 2005
    Posts
    10
    Thanks, I've changed the code to the following:

    if (strcmp(argv[1],"-e") == 0) {
    printf("test e");
    } else if (strcmp(argv[1],"-d") == 0) {
    printf("test d");
    } else {
    printf("No mode selected");
    }
    it works perfectly now, thank you for the quick reply : )

  5. #5
    Registered User
    Join Date
    Oct 2009
    Location
    While(1)
    Posts
    377
    You are most welcome and enjoy C/C++ programming

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. conditional compilation based on existance of a symbol?
    By cyberfish in forum C++ Programming
    Replies: 6
    Last Post: 12-13-2007, 06:35 PM
  2. Screwy Linker Error - VC2005
    By Tonto in forum C++ Programming
    Replies: 5
    Last Post: 06-19-2007, 02:39 PM
  3. problem with floats in conditional statements
    By Desolation in forum C Programming
    Replies: 3
    Last Post: 07-08-2006, 01:56 AM
  4. NAQ: Everything you never wanted to know about CPP
    By evildave in forum C Programming
    Replies: 21
    Last Post: 12-12-2005, 09:56 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21