title: Using "set /p" in C++
body: <p>I'm trying to set a windows command line environment variable (to help automate starting a windows process requiring user input) and then echo it back via system().</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      system("set /p foo= foo=");
      system("echo %foo%");
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>Upon running, I receive this feedback:</p>
  
  <blockquote>
    <p>foo=bar</p>
    
    <p>%foo%</p>
    
    <p>Press any key to continue . . .</p>
  </blockquote>
  
  <p>For the program to work, I need to receive "bar", the content of %foo%, but this is not happening.</p>
  
  <p>What is strange is that the code works if inputted directly into the command line.</p>
  
  <pre><code>C:\Users\JohnDoe&gt;set /p foo= foo=
  foo=bar
  
  C:\Users\JohnDoe&gt;echo %foo%
  bar
  </code></pre>
  
  <p>Why does the C++ code not work?</p>
  
  <p>EDIT:</p>
  
  <p>Using the suggested change, as follows:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      system("set /p foo=\" Enter foo= \" &amp; echo %foo%");
      system("pause");
      return 0;
  }
  </code></pre>
  
  <p>Returns this feedback:</p>
  
  <blockquote>
    <p>Enter foo= bar</p>
    
    <p>%foo%</p>
    
    <p>Press any key to continue . . .</p>
  </blockquote>
  
  <p>Using the following code works, but opens a second window:</p>
  
  <pre><code>#include &lt;iostream&gt;
  using namespace std;
  
  int main() {
      system("set /p foo=\" Enter foo= \" &amp; start echo %foo%");
      system("pause");
      return 0;
  }
  </code></pre>
  
  <blockquote>
    <p>Enter foo= bar
    Press any key to continue . . .</p>
  </blockquote>
  
  <p>Then in the second window:</p>
  
  <blockquote>
    <p>bar</p>
  </blockquote>