简体   繁体   中英

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.

For example in cmd

Myprograme.exe 11 32 232 So that it uses these values in the calculation.

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:

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).

argv is an array of nul-terminated character strings, each of which contains the appropriate command-line argument. argv[argc] is a null pointer.

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
  • argv[0] will be "myprog.exe"
  • argv[1] will be "a"
  • argv[2] will be "bb"
  • argv[3] will be "c"
  • argv[4] will be the null pointer

The main method can have two arguments :

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. You can then convert you char* to float as you want. 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. This means you get char-arrays instead of floats which is absolutely understandable because you might also write

Myprograme.exe ab cde fg

See Converting char* to float or double for how to convert char* to float.

Answer to your first question.
This is called 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

@danial weaber this is a very good example, but it does not run to the right sum. That is because it is not finding 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.

Also, some ide's may not run your code correctly. A lot of times using notepadd++ then compiling your code in the command line, you may get the correct results. Good Luck

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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