I have a program in C++ which I run for many values of a parameter. What I want to do is the following: Let's say I have two parameters as:
int main(){
double a;
double b;
//some more lines of codes
}
Now after after I compile I want to run it as
./output.out 2.2 5.4
So that a
takes the value 2.2 and b
takes the value 5.4.
Of course one way is to use cin>>
but I cannot do that because I run the program on a cluster.
You need to use command line arguments in your
main
:This code parses parameters using
atof
; you could usestringstream
instead.If you want to use command line parameters then no, you don't use
cin
as it is too late. You need to change yourmain
signature to:So you now have
argv
which is an array of pointer tochar
, each pointer pointing to an argument that was passed. The first argument is typically the path to your executable, subsequent arguments are whatever was passed in when your application was launched, in the form of pointers tochar
.You will need to convert the
char*
's todouble
s in this case.That's what command-line arguments are for:
You can use this form of the
main()
function to get command line argumentswhere the values of the
argv[]
array contain your command line variables aschar*
that you will need to convert tofloats
ordoubles
in your caseHave you looked at boost program options?
It will take the command line arguments like many other are suggesting and let you provide a very consistent, clean and extensible command line interface.