简体   繁体   English

命令行参数

[英]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 . 我遇到的问题是给定的命令行参数为== 3 If 3 arguments are given then either the terminal is suppose to print out... 如果给出了3个参数,则终端都应该打印出...

Hi, Mr. last_name, your test score is test_score 嗨,last_name先生,您的考试成绩为test_score

...or... ...要么...

My name is first_name last_name. 我的名字是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... 如果仅在命令行参数中输入姓氏和测试分数(史密斯3.4),则将其打印出来(例如使用姓氏Smith的示例),然后将其打印出来...

My name is Smith 3.4 我叫史密斯3.4

However, it does work for putting in the first name and last name (John Smith). 但是,它确实可以用于输入名字和姓氏(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. 我不想要答案 ,我只想要我做错了什么,并提示如何解决它。

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. 问题1:在初始化之前,您正在使用testscore变量。

Problem 2: You are not performing error handling with atof . 问题2:您没有使用atof执行错误处理。 I would suggest to use strtod() . 我建议使用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. 您也可以创建自己的atof()实现,该实现将转换并报告转换中的错误(如果有)。

Hint: Try to first check that the number of arguments passed to the c program. 提示:尝试首先检查传递给c程序的参数数量。 After that, try to convert third argument to float using strtod() or your own implementation. 之后,尝试使用strtod()或您自己的实现将第三个参数转换为float。 If it successfully converts, assign the result of float convrsion to test_score and print last_name and testscore . 如果转换成功,则将float convrsion的结果分配给test_score并输出last_nametestscore If not, then consider third argument as last_name and print first_name and last_name . 如果不是,则将第三个参数视为last_name并打印first_namelast_name

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. 实际上,当argc == 3时,您要检查argv [2]是否为数字参数。

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". 我没有测试代码,可能有更好的方法来检查命令行中的参数是否为“数字”。

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);
}

... ...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM