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.
They are parameters/arguments supplied to the program when it is invoked. They are used to control program from outside instead of hard coding those values inside the code. argv[argc] is a NULL pointer. argv[0] holds the name of the program.
To take input (or arguments) from the command line, the simplest way is to use another one of Python's default modules: sys. Specifically, you're interested in sys. argv, a list of words (separated by space) that's entered at the same time that you launch the script.
Python provides developers with built-in functions that can be used to get input directly from users and interact with them using the command line (or shell as it is often called). In Python 2, raw_input() and in Python 3, we use input() function to take input from Command line.
To get the user's input from the web browser is easy by using JavaScript prompt() method. However, what is not understandable is using JavaScript which runs only the browser, to get input from a systems command line. Doing this is possible using NodeJS.
You need to use command line arguments in your main
:
int main(int argc, char* argv[]) {
if (argc != 3) return -1;
double a = atof(argv[1]);
double b = atof(argv[2]);
...
return 0;
}
This code parses parameters using atof
; you could use stringstream
instead.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With