1

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
3
  • You can input an array of characters only. Commented Jan 17, 2020 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 Commented Jan 17, 2020 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 Commented Jan 17, 2020 at 11:53

1 Answer 1

4

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?

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.