Thread: Trouble with command-line arguments in C code (Linux)

  1. #1
    Registered User
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6

    Trouble with command-line arguments in C code (Linux)

    Hi,

    The following is an excerpt from a simple unit converter program I am writing. It compiles with no errors, but the code within the if statement under // Convert units, is not run. I have checked that argv[ARG_CONV_FROM] and argv[ARG_CONV_TO] actually do contain the correct data. I assume there must be some incompatibillity issue between the data contained in argv and the "celcius" and "fahrenheit" strings, but I cannot find the error. Please help

    Code:
    ...
    
    int main(int argc, char *argv[]) {
    	// Variables and constants;
    	double result = 0;
    	const int REQUIRED_ARGS = 4;
    	const int ARG_VALUE = 1;
    	const int ARG_CONV_FROM = 2;
    	const int ARG_CONV_TO = 3;
    	
    	// Check user input
    	if (argc != REQUIRED_ARGS) {
    		show_usage();
    		exit(1);
    	}
    	
    	// Convert units
    	if ((argv[ARG_CONV_FROM] == "celcius") && (argv[ARG_CONV_TO] == "fahrenheit")) {
    		result = atoi(argv[ARG_VALUE]) * (9 / 5) + 32;
    	}
    
    	printf("Result = %f\n", result);
    	return 0;
    }
    
    ...
    Using GCC 4.6.0 on Linux Fedora 15

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    You can't compare strings with ==. That's what strcmp is for.


    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User
    Join Date
    Jul 2011
    Location
    Norway
    Posts
    6
    Thanks Much appreciated

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Command line arguments
    By Jasper in forum C Programming
    Replies: 8
    Last Post: 08-07-2009, 10:24 AM
  2. command line arguments
    By benhaldor in forum C Programming
    Replies: 17
    Last Post: 08-25-2007, 07:04 AM
  3. command line arguments
    By belusha in forum C Programming
    Replies: 4
    Last Post: 01-27-2003, 09:35 PM
  4. command line arguments
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 10-20-2001, 11:07 AM
  5. command line arguments
    By Unregistered in forum C Programming
    Replies: 2
    Last Post: 09-26-2001, 09:22 PM