SoonPro
SoonPro

Reputation: 121

Command Line Argument

I am having trouble figuring out how to make this code work. What is suppose to is depending on the arguments giving from the command line, it suppose to print out a greeting.

int main (int argc, char *argv[]) {

double testscore;

    if (argc == 2) {
        printf("Hello, Mr.%s.\n", argv[1]);

    }
    else if (argc == 3 && argc == testscore) {
        testscore = atof(argv[2]);
        printf("Hi, Mr.%s, your score is %.1f\n", argv[1], testscore);
    }
    else  {

        printf("My name is %s %s.\n", argv[1], argv[2]);
    }
}

If someone puts only their last name, then the terminal will print out...

Hello, Mr. last_name

...because they only put in one argument. This works fine.

The part where I am stuck on is when the command line arguments given are == 3. If 3 arguments are given then either the terminal is suppose to print out...

Hi, Mr. last_name, your test score is test_score

...or...

My name is first_name last_name.

If I put in the command line arguments only the last name and test score (Smith 3.4) then it prints out (example using the last name Smith) then it prints out...

My name is Smith 3.4

However, it does work for putting in the first name and last name (John Smith). This gives...

My name is John Smith.

I do not want the answer, I just want what I am doing wrong and hints on how to fix it.

Upvotes: 2

Views: 213

Answers (3)

SoonPro
SoonPro

Reputation: 121

Got it the answer guys. To check it without using another function, it would be

....

else if ( argc==3 && sscanf(argv[2], "%f", testscore) 


{


    testscore = atof(argv[2]);

    printf("Hi, Mr.%s, your score is %.1f\n", argv[1], testscore);
}

...

Upvotes: 0

abhiarora
abhiarora

Reputation: 10430

I do not want the answer, I just want what I am doing wrong and hints on how to fix it.

Problem 1: You are using testscore variable before it is being initialized.

Problem 2: You are not performing error handling with atof. I would suggest to use strtod(). You can perform some error handling with it to know that the third argument is a float or not. You can also create your own implementation of atof() which will convert and report error in conversion, if any.

Hint: Try to first check that the number of arguments passed to the c program. After that, try to convert third argument to float using strtod() or your own implementation. If it successfully converts, assign the result of float convrsion to test_score and print last_name and testscore. If not, then consider third argument as last_name and print first_name and last_name.

Upvotes: 3

ssinfod
ssinfod

Reputation: 1051

Your problem is with this line:

else if (argc == 3 && argc == testscore) {

In fact, when argc == 3, then you want to check if argv[2] is a numeric argument.

else if ( (argc==3) && (is_numeric_arg(argv[2])==1)) {

A possible implementation would be:

int is_numeric_arg(char* arg)
    {
        int isInt = 0;
        int isFloat = 0;
        int isChar = 0;
        char* currChar;
        int i = 0;
        currChar = arg;
        isInt = 1;
        while (*currChar != '\0')
        {
            if(*currChar < '0' || *currChar > '9')
            {
                if(*currChar == '.' && isInt == 1)
                {
                    isInt = 0;
                    isFloat = 1;
                }
                else
                {
                    isInt = 0;
                    isChar = 1;
                }
            }
            currChar++;
        }
        if (isChar == 1){ return 0; } // argument is a string
        else { return 1; } // argument is a int or float
    }

int main (int argc, char *argv[]) {

    double testscore;

    if (argc == 2) {
        printf("Hello, Mr.%s.\n", argv[1]);

    }
    else if ( (argc==3) && (is_numeric_arg(argv[2])==1)) {
        testscore = atof(argv[2]);
        printf("Hi, Mr.%s, your score is %.1f\n", argv[1], testscore);
    }
    else  {

        printf("My name is %s %s.\n", argv[1], argv[2]);
    }
}

I did not test the code and there is probably a better way to check that the argument from the command line is "numeric".

Upvotes: 2

Related Questions