title: Print accumulated sums in C++ in a recursive function
body: <p>So I have this function and these global variables.</p>
  
  <pre><code>int recurs=0;
  std::string sign="";
  void count2(int k, std::vector&lt;int&gt; d, int total, int temp, bool flag, unsigned short int pos){
      std::string mas="+";
      std::string menos="-";
      if(pos==(d.size())){
          total+=temp;
          if(total==k){
              result++;
              std::cout &lt;&lt; sign &lt;&lt; "=" &lt;&lt; k&lt;&lt;std::endl;
              str="";
          }
          recurs++;
          return;
      }    
      //Sum sign.
      sign=sign.substr(0,sign.size()-recurs*2);
      sign.append(mas+=std::to_string(d[pos]));
      count2(k,d,total+temp,+d[pos],true,pos+1);  
      //Rest sign
      sign=sign.substr(0,sign.size()-recurs*2);
      sign.append(menos+=std::to_string(d[pos]));
      count2(k,d,total+temp,-d[pos],false,pos+1);  
      //Append digit
      if(flag==true)
          count2(k,d,total,10*temp-d[pos],true,pos+1);
      else
          count2(k,d,total,+10*temp+d[pos],false,pos+1);
  }
  </code></pre>
  
  <p>The function is called like this:
  <code>count2(6,{1,2,3,3,3},0,0,true,0);</code></p>
  
  <p><strong>What it does:</strong> Given a vector v, it makes combinations of sums and substractions and numbers and everytime that this combination equals the first parameter, a global variable <code>result</code> is increased. For example, <code>count2(6,{1,2,3,3,3},0,0,true,0);</code> would make <code>result</code> be 5. Since there is 5 ways to sum/sub 6 with those numbers, e.g.: <code>1+2+3+3-3</code> <code>1+2-3+3+3</code> and some others. It works perfectly. Also, the variable string <code>str</code> is not used.</p>
  
  <p><strong>What is wrong?</strong> Nothing, but I'd like to see which these combinations are. I'd like the function to print thinks like:</p>
  
  <pre><code>1+2+3+3-3
  1+2-3+3+3
  -1-2+3+3+3
  1+2+3-3+3
  </code></pre>
  
  <p><strong>What is the question?</strong> I'd like to know how to correctly print the operatins that make total be equal to k. 
  If you were to <a href="http://ideone.com/aOR2TM" rel="nofollow">run this code in ideone</a>, it would print: YES, This is my actual outcome. But is not correct since there's no sum such as +3 or +3+3+3+3+3+3+....</p>
  
  <pre><code>+1+2+3+3-3=6
  +3=6
  +3-3+3-3-3+3+3=6
  +3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-1+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3=6
  +3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-1+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3+2+3+3+3-3-3+3-3+3-3-3+3+3-3-3+3-3+3-3+3+3-3-3+3-3+3-3-2+3+3+3=6
  </code></pre>
  
  <p><strong>The correct outcome</strong> <em>could</em> look like: </p>
  
  <pre><code>1+2+3+3-3
  1+2+3-3+3
  1+2-3+3+3
  </code></pre>
  
  <p>-1-2+3+3+3</p>
  
  <p><strong>Question answered!</strong></p>