简体   繁体   English

如何使用数组接受命令行参数?

[英]How to use an array to take command line arguments?

So I'm trying to use cmd to take arguments while starting the program, but I keep running into issues from converting the variables from char to int, so they can be used with the rest of the program. 因此,我试图在启动程序时使用cmd接受参数,但是我一直遇到将变量从char转换为int的问题,因此它们可以与程序的其余部分一起使用。 Thanks for the help ahead of time. 感谢您的提前帮助。

int main(int argc, char* argv[]) {
    int n = 0, a = 0, b = 0, func = 0, choice = 0;
    double w;
    if(argc < 10)
    {
        cout << "The Program requires -f <Function> -a <Starting Point> -b <Ending Point> -m <Method> -n <Number of slices>" << endl;
        exit(0);
    }
    else
    {
        int func, a, b, choice, n;
        int i;
        for(int i = 1; i < argc - 1; i+=2)
        {
                if(argv[i] == "-f"){
                    func = *argv[i + 1];}
                else if(argv[i] == "-a"){
                    a = *argv[i + 1];}
                else if(argv[i] == "-b"){
                    b = *argv[i + 1];}
                else if(argv[i] == "-m"){
                    choice = *argv[i + 1];}
                else if(argv[i] == "-n"){
                    n = *argv[i + 1];}
                else {
                    cout << "Input is Invalid" << endl;
                    exit(0);}
                cout << argv[i] << endl;
        }
        w = (b-a)/double(n);
        next(n, a, b, w, func, choice);
    }
    return 0;
}

edit: So I have restructured my code to look like that above and it does compile, but I'm getting only the else statement. 编辑:因此,我已经将代码重组为上面的样子,并且可以编译,但是我只得到了else语句。 Such as when is start the program using a line like: ./a3 -f 5 -a 1 -b 10 -m 3 -n 100. I'm not sure why the program reverts to the else statement "input is Invalid" instead of the other if, if else statements. 例如,何时使用以下行启动程序:./a3 -f 5 -a 1 -b 10 -m 3 -n100。我不确定为什么程序会恢复为else语句“ input is Invalid”其他if语句。

Dude, you have to start with: 杜德,您必须从以下内容开始:

int n = 0, a = 0, b = 0, func = 0, choice = 0;

and then you have a character pointer to an integer type defined as 然后你有一个指向整数类型的字符指针,定义为

  char* func, a, b, choice, n;

I believe in the standard, pointer should point to their type and that is your problem. 我相信在标准中,指针应该指向它们的类型,这就是您的问题。

Solution: 解:

I guess you could use cast it to an char before you assign the ptrs.. 我猜您可以在分配ptrs之前将其转换为char。

you have problem with string comparing. 你有比较字符串的问题。

if(argv[i] == "-f")

don't check if argv[i] is equal to "-f" it check if the pointer value is the same as the pointer to the constants string "-f" with is wrong. 不要检查argv [i]是否等于“ -f”,而是检查指针值是否与指向常量字符串“ -f”的指针相同。 you should use string compare methods like strcmp. 您应该使用字符串比较方法,例如strcmp。

also is all flags require value why the loop is: 也是所有标志都需要值,为什么循环是:

for(int i = 1; i < argc; i++)
    {
    if(i + 1 != argc)

because it pairs you can write it as: 因为可以配对,所以可以这样写:

for(int i = 1 ; i < argc - 1; i += 2) 

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

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