Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Help in C program (http://www.go4expert.com/forums/help-c-program-t4140/)

munkyeetr 4May2007 23:08

Help in C program
 
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 */
}


DaWei 5May2007 04:29

Re: Help in C program
 
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.

munkyeetr 5May2007 09:11

Re: Help in C program
 
Thanks, DaWei.

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

Code: c

#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 */
}

:)

DaWei 5May2007 16:09

Re: Help in C program
 
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.


All times are GMT +5.5. The time now is 20:03.