1. We have moved from vBulletin to XenForo and you are viewing the site in the middle of the move. Though the functional aspect of everything is working fine, we are still working on other changes including the new design on Xenforo.
    Dismiss Notice

Help in C program

Discussion in 'C' started by munkyeetr, May 4, 2007.

  1. munkyeetr

    munkyeetr New Member

    Hi everyone,

    When I pass any of the following (-help, help, ?, /?) to this program, I expect it execute the PrintUsage() function, but it doesn't.

    I have tested the value of argv[1] before the switch statement, and it showed the correct value, but when it gets to case 2's if/else statement it seems to

    return false on the if.

    Can anyone tell me what I am doing wrong?

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    
    void PrintUsage(void);
    
    int main(int argc, char* argv[]) {
    
    /* holds the sum */
        double dAnswer = 0.0;
    
    /* parse input */
        switch(argc) {
    
        /* no input parameters */
    	    case 1:
    	        PrintUsage();
    	        break;
    
        /* only 1 parameter passed; return it */
    	    case 2:
    		if ((argv[1]=="--help") || (argv[1]=="help") || (argv[1]=="?") || argv[1]=="/?") {
    			PrintUsage();
    		}
    		else {
    	        	printf("%.2f\n", atof(argv[1]));
    		}
                	
    		break;
    
        /* multiple parameters passed; calculate the sum */
    	    default: {
            /* a counter */
    	        int iCount = 0;
            /* cycle through input, summing the values */
    	        for (iCount = 1; iCount < argc; iCount++) {
    	            dAnswer += atof(argv[iCount]);
    	        }
            /* print sum to the screen */
    	        printf("%.2f\n", dAnswer);
    	    }
    	}
    /* end program */
        return 0;
    }
    
    void PrintUsage(void) {
        /* code removed to conserve space */
    }
    
     
  2. DaWei

    DaWei New Member

    C strings are arrays of char, terminated by a nul ('\0'). They are not an object, but a collection of smaller objects. One cannot compare them with == or assign to them with =. Include <string.h> and use the functions there (strcmp, strcpy, strcat, etc.) to manipulate them.

    Do not let the valid statements,
    Code:
        char *myString = "This is a C string";
        char  myString [20] = "So is this";
        int myArray [4] = {1,2,3,4};
    
    fool you. Those are not executable statements. They are favors the compiler does for you at compile time, and are for initialization purposes only.
     
  3. munkyeetr

    munkyeetr New Member

    Thanks, DaWei.

    I played with the code and it now compiles and runs like it should.

    Code:
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    void PrintUsage(void);
    
    int main(int argc, char* argv[]) {
    
    /* holds the sum */
        double dAnswer = 0.0;
    	
        
    /* parse input */
        switch(argc) {
    
        /* no input parameters */
    	    case 1:
    		PrintUsage();
    	        break;
    
        /* only 1 parameter passed */
    	    case 2: {
    		
    		int inplen = strlen(argv[1]);
    	    	char input[inplen];
    		strcpy(input, argv[1]);
    
    	    /* check for usage switches */
    		if ( !strcmp(input, "-help") ||	!strcmp(input, "help") || !strcmp(input, "?") || !strcmp(input, "/?") )
    	    /* call PrintUsage function if a valid switch is present */		
    			PrintUsage();
    		else
    	    /* return the single value if it is numeric */
    			printf("%.2f\n", atof(argv[1]));	
    		}		
    	        break;
    
        /* multiple parameters passed; calculate the sum */
    	    default: {
            /* a counter */
    	        int iCount = 0;
            /* cycle through input, summing the values */
    	        for (iCount = 1; iCount < argc; iCount++) {
    	            dAnswer += atof(argv[iCount]);
    	        }
            /* print sum to the screen */
    	        printf("%.2f\n", dAnswer);
    	    }
    	}
    
    /* end program */
        return 0;
    }
    
    void PrintUsage(void) {
        /* code removed to conserve space */
    }
    
    :)
     
  4. DaWei

    DaWei New Member

    Code:
    char input[inplen];
    
    This statement (as well as declarations in the body of the code) will work only with compilers that are C99 compliant in those regards, which makes your code not very portable.
     

Share This Page