0

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
anastaciu
  • 23,467
  • 7
  • 28
  • 53
  • You can input an array of characters only. – asmmo Jan 17 '20 at 11:44
  • 1
    What you're looking for is called "input stream redirection" or "input stream piping". I'm not sure how it works under windows, should be something like `echo 1 | out.exe` – Denis Sheremet Jan 17 '20 at 11:51
  • 1
    There's also an option to use command-line arguments instead. If you'll update `main` signature to `int main(int argc, char** argv)`, `argv` will be a c-string array containing all arguments you've provided after `out.exe`, and `argc` would be their count – Denis Sheremet Jan 17 '20 at 11:53

1 Answers1

3

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?

anastaciu
  • 23,467
  • 7
  • 28
  • 53