Mohammed S. Yaseen
Mohammed S. Yaseen

Reputation: 139

Is it possible to pass input arguments to an executable in the command line

let's say I have this c++ code which I compiled into an executable out.exe

int main(){   
   int a = 0;  
   cin>>a;

   if (a)    
      cout<<"done";  

   return 0;  
}

Normally I would execute it using the command line by typing its name, then it would wait for my input.

However, what I want it to pass the input in the same line that I am calling the executable like so:

out.exe 1

1 being the input so the program wouldn't wait for my input and directly outputs:

done

Upvotes: 0

Views: 3105

Answers (1)

anastaciu
anastaciu

Reputation: 23792

You can use int main(int argc, char **argv) command line arguments **argv, and argument counter argc look in What does int argc, char *argv[] mean?.

The arguments are read as strings but you can easily convert them into the type you need, in this case int. How can I convert a std::string to int?

Upvotes: 3

Related Questions