user1275860 user1275860 - 3 months ago 36
C++ Question

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.


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.

Answer Source

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
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download