简体   繁体   English

使用CMD中指定的变量或其他程序的系统调用从命令行调用c ++

[英]Calling c++ from command line with variables specified in CMD or from the system call of another program

Hi I am relatively new to programing. 嗨,我对编程比较陌生。

I want to create a C++ program that when you call it in CMD you can pass it variables. 我想创建一个C ++程序,当你在CMD中调用它时,你可以传递它的变量。

For example in cmd 例如在cmd中

Myprograme.exe 11 32 232 So that it uses these values in the calculation. Myprograme.exe 11 32 232这样它就可以在计算中使用这些值。

C++ C ++

int main(float A, float B, float C){
float sum= A+B+C;
cout << sum;
return 0;
}

My problem is I don't know what you would call this process to even Google it. 我的问题是我不知道你会把这个过程称为谷歌它。

The standard signature of main is as follows: 标准签名main是如下:

int main(int argc, const char **argv)

argc is the number of comman-line arguments that were given to the program (including argument number 0 which is the program's name). argc是给予程序的命令行参数的数量(包括参数号0,即程序的名称)。

argv is an array of nul-terminated character strings, each of which contains the appropriate command-line argument. argv是一个以空字符结尾的字符串数组,每个字符串都包含相应的命令行参数。 argv[argc] is a null pointer. argv[argc]是一个空指针。

You can use these to parse the command-line arguments an pass them on to your computation. 您可以使用它们来解析命令行参数,并将它们传递给您的计算。

For example, if you issue the following on the command line: 例如,如果在命令行上发出以下命令:

myprog.exe a bb c
  • argc will be 4 argc将是4
  • argv[0] will be "myprog.exe" argv[0]将是"myprog.exe"
  • argv[1] will be "a" argv[1]将是"a"
  • argv[2] will be "bb" argv[2]将是"bb"
  • argv[3] will be "c" argv[3]将是"c"
  • argv[4] will be the null pointer argv[4]将是空指针

The main method can have two arguments : main方法可以有两个参数:

int main(int argc, char** argv)
{
}

argc is the number of arguments, and argv is an array of char* containing the value of each argument. argc是参数的数量, argv是包含每个参数值的char *数组。 You can then convert you char* to float as you want. 然后,您可以根据需要将char *转换为float。 Take care, the first argument is the name of the program itself. 注意,第一个参数是程序本身的名称。

You always have a main function like 你总是有一个像这样的主要功能

int main(int argc, char **argv)
{
}

The first argument is the number of arguments, argv points to argc char* s which are the arguments. 第一个参数是参数的数量,argv指向argc char* s,它们是参数。 This means you get char-arrays instead of floats which is absolutely understandable because you might also write 这意味着你得到char数组而不是浮点数,这是绝对可以理解的,因为你也可以写

Myprograme.exe ab cde fg Myprograme.exe ab cde fg

See Converting char* to float or double for how to convert char* to float. 请参阅将char *转换为float或double以了解如何将char *转换为float。

Answer to your first question. 回答你的第一个问题。
This is called command line argumants . 这称为command line argumants
You can use this keyword to google for it. 您可以使用此关键字来谷歌。

This is what you tried to do. 这就是你想要做的。 First define the main function like this. 首先像这样定义主函数。

int main(int argc, char *argv[]) {
float sum,a,b,c;
a=atof(argv[0]);
b=atof(argv[1]);
c=atof(argv[2]);
sum=a+b+c;
cout<<sum;
}

Now you can pass the arguments Myprograme.exe 11 32 232 it will return 275 现在你可以传递参数Myprograme.exe 11 32 232它将返回275

@danial weaber this is a very good example, but it does not run to the right sum. @danial weaber这是一个非常好的例子,但它没有运行到正确的总和。 That is because it is not finding c. 那是因为它找不到c。

Your example could should be: 你的例子应该是:

    #include <iostream>
    using namespace std;

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

        float sum,a,b,c;

        sum=atof(argv[0]);
        a=atof(argv[1]);
        b=atof(argv[2]);
        c=atof(argv[3]);

        sum=a+b+c;

        cout<<sum;
    }

Just wanted to point that out, so that when they run it with... Myprograme.exe 11 32 232 it will then return 275. 只是想指出这一点,所以当他们运行它... Myprograme.exe 11 32 232它将返回275。

Also, some ide's may not run your code correctly. 此外,某些ide可能无法正确运行您的代码。 A lot of times using notepadd++ then compiling your code in the command line, you may get the correct results. 很多时候使用notepadd ++然后在命令行中编译代码,您可能会得到正确的结果。 Good Luck 祝好运

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

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