title: store the value of cout
body: <p>I have the following code</p>
  
  <pre><code>#include &lt;iostream&gt;
  #include &lt;math.h&gt;
  #include &lt;string&gt;
  #include &lt;sstream&gt;
  using namespace std;
  int main() {
  int value
  cout &lt;&lt; "Input your number: " &lt;&lt; endl;
  cin &gt;&gt; value;
  string s = to_string(value);
  const int count = s.length();
  int position = count;
  for (int i = 1; i &lt; count + 1; i++)
  {
      int pwr = pow(10, position - 1);
      cout &lt;&lt; ((value / pwr) + position) % 10;
      position--;
      value = value % pwr;
  }
  </code></pre>
  
  <p>Instead of cout, how can i store the value of<code>((value / pwr) + position) % 10</code> into a variable using the for loop. Thank you very much for the help.</p>
  
  <p>[edit]
  I added an array instead</p>
  
  <pre><code>int val[7];
  int position = count;
  for (int i = 1; i &lt; count + 1; i++)
  {
      int pwr = pow(10, position - 1);
      val[i-1] = ((value / pwr) + position) % 10;
      position--;
      value = value % pwr;
  }
  cout &lt;&lt; "Encoded value is: ";
  for (int i = 0; i &lt; 8; i++)
  {
      cout &lt;&lt; val[i];
  }
  </code></pre>
  
  <p>It was able to output the values I want, but there is a Run-Time Failure #2 - stack around the variable 'val' was corrupted. Why is that?</p>