Print All Subvector Combinations + Issue
<p>I am attempting to create a tool that will make my life easier for setting up a list of configuration settings based on numerical addition to indicate which features should be enabled.  I am using a power of 2 list from 1 to 512 (1,2,4,8,16,32,64,128,256,512).</p>
<p>Manually going through and creating a list of which values will make which features enabled would be much too time consuming and so I am trying to do it programmatically then save the output into a file.  I am, however, running into problems finding a proper solution.</p>
<p>I have read just about every question from SO and other coding forums that have come up in Google on the topic and all of them address linear combinations.  This I am able to do and I have this example code that I altered and then built my tool on top of:</p>
<pre><code>    #include &lt;iostream&gt;
using std::cout;
using std::cin;
using std::endl;

// A is the array that contains the numbers
// comb is an array of size k that will hold all possible combinations
// n is the size of input array
// k is 1 less than the size of combination i.e. we want to find out 4C2 k =1
// current_k is the variable that makes us simulates k loops in a recursive function
void combinations(int A[], int comb[], int start, int n, int current_k, int k){
    int sum = 0;

    if (k &lt; 0)
        return;

    // Base case just print all the numbers 1 at a time
    if (k == 0){
        for (int i = 0; i &lt; n; i++)
            cout &lt;&lt; A[i] &lt;&lt; endl;
    }

    // current_k goes from 0 to k-1 and simulates a total of 
    // k iterations
    if (current_k &lt; k){
        // if current_k = 0, and k = 3 (i.e. we need to find combinations of 4) 
        // then we need to leave out 3 numbers from the end because there are 3
        // more nested loops
        for (int i = start; i &lt; n - (k - current_k); i++){
            // Store the number in the comb array and recursively call with the remaining sub-array
            comb[current_k] = A[i];
            // This will basically pass a sub array starting at index 'start' and going till n-1
            combinations(A, comb, i+1, n, current_k+1, k);
        }
    }

    else if (current_k == k){
        for (int i = start; i &lt; n; i++){
            comb[current_k] = A[i];

            for (int j = 0; j &lt;= k; j++){
                sum += comb[j];     
            }
            
            cout &lt;&lt; sum &lt;&lt; endl;
            sum = 0;
        }
    }

    else
        return;
}

int main(){
    int n;
    cout &lt;&lt; &quot;Enter the 'n' &quot; &lt;&lt; endl;
    cin &gt;&gt; n;
    int *A = new int[n];

    for (int i = 0; i &lt; n; i++)
        A[i] = i+1;

    int k;
    cout &lt;&lt; &quot;Enter 'k'&quot; &lt;&lt; endl;
    cin &gt;&gt; k;
    int *comb = new int[k];

    combinations(A, comb, 0, n, 0, k-1);

    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
<p>The only problem is that I also need the non-linear combinations.  Things like 1+64+256.  Will this also be able to pick up these combinations?  And I am also having an issue which I will explain after I post my code.  Here is the actual code I am using for this:</p>
<pre><code>#include &lt;iostream&gt;
#include &lt;vector&gt;
#include &lt;string&gt;
#include &quot;mcl.h&quot;

using std::cout;
using std::vector;
using std::string;
using std::cin;
using std::endl;

static vector&lt;string&gt; results;

// A is the array that contains the numbers
// comb is an array of size k that will hold all possible combinations
// n is the size of input array
// k is 1 less than the size of combination i.e. we want to find out 4C2 k =1
// current_k is the variable that makes us simulates k loops in a recursive function
void combinations(vector&lt;mcl&gt; A, vector&lt;mcl&gt; comb, int start, int n, int current_k, int k){
    string sum;
    string sNames;
    int sCodes = 0;

    if (k &lt; 0)
        k = 0;

    // Base case just print all the numbers 1 at a time
    if (k == 0){
        for (int i = 0; i &lt; n; i++)
            cout &lt;&lt; A.at(i).getCode() &lt;&lt; &quot; - &quot; &lt;&lt; A.at(i).getName() &lt;&lt; endl;
        return;
    }

    // current_k goes from 0 to k-1 and simulates a total of 
    // k iterations
    if (current_k &lt; k){
        // if current_k = 0, and k = 3 (i.e. we need to find combinations of 4) 
        // then we need to leave out 3 numbers from the end because there are 3
        // more nested loops
        for (int i = start; i &lt; n - (k - current_k); i++){
            // Store the number in the comb array and recursively call with the remaining sub-array
            comb.push_back(mcl(A.at(i).getCode(),A.at(i).getName()));
            // This will basically pass a sub array starting at index 'start' and going till n-1
            combinations(A, comb, i + 1, n, current_k + 1, k);
        }
    }

    else if (current_k == k){
        for (int i = start; i &lt; n; i++){
            comb.at(current_k-1) = A.at(i);

            for (int j = 0; j &lt; k; j++){
                sCodes += comb.at(j).getCode();
                if (sNames != &quot;&quot;){
                    sNames = sNames + &quot;,&quot; + comb.at(j).getName();
                }

                else{
                    sNames = sNames + comb.at(j).getName();
                }
            }
        }

        results.push_back(sCodes + &quot; - &quot; + sNames);
        sCodes = 0;
        sNames = &quot;&quot;;
    }

    else
        return;
}

int main(){
    int k;
    vector&lt;mcl&gt; A,comb;

    A.push_back(mcl(1, &quot;Light&quot;));
    A.push_back(mcl(2, &quot;Bright&quot;));
    A.push_back(mcl(4, &quot;Dark&quot;));

    k = 2;
    

    combinations(A, comb, 0, A.size(), 0, k - 1);

    //system(&quot;cls&quot;);
    for (int i1 = 0; i1 &lt; results.size(); i1++){
        cout &lt;&lt; results.at(i1) &lt;&lt; endl;
    }

    system(&quot;pause&quot;);
    return 0;
}
</code></pre>
<p>mcl header and imp code:</p>
<pre><code>#ifndef MCL_H
#define MCL_H

#include &lt;string&gt;

using std::string;

class mcl{
public:
    mcl(int code, string name);

    int getCode();
    string getName();

    void setCode(int i);
    void setName(string s);

private:
    int cCode;
    string cName;
};
#endif;
</code></pre>
<p>imp:</p>
<pre><code>#include &quot;mcl.h&quot;;

mcl::mcl(int code, string name){
    cCode = code;
    cName = name;
}

int mcl::getCode(){
    return cCode;
}

string mcl::getName(){
    return cName;
}

void mcl::setCode(int i){
    cCode = i;
}

void mcl::setName(string s){
    cName = s;
}
</code></pre>
<p>Now for the issue.  When I am attempting to test the display of the combinations of the three mcl objects I have defined in the code I am seeing this output:</p>
<blockquote>
<p>Bright,Dark</p>
<p>,Dark</p>
<p>Press any key to continue . . .</p>
</blockquote>
<p>As I am seeing each of the individual object's data showing correctly if I set k as 0:</p>
<blockquote>
<p>1 - Light</p>
<p>2 - Bright</p>
<p>4 - Dark</p>
<p>Press any key to continue . . .</p>
</blockquote>
<p>I think this issue is being caused by the for loop that is creating the results vector for display but I am unsure of what the actual problem is.</p>
<p>As for an example of my expected output, given the hard coded elements in the code above this is what I am trying to get the tool to output (where bold are base settings and non-bolded are combinations):</p>
<p><strong>1 - Light<br></strong>
<strong>2 - Bright<br></strong>
3 - Light,Bright<br>
<strong>4 - Dark<br></strong>
5 - Light,Dark<br>
6 - Bright,Dark<br>
7 - Light,Bright,Dark</p>