A little next combination program in C++
using std::cout;, while better than including the entire namespace, is still worse than simply using the std:: prefix. If typing it annoys you too much, you can just make a macro in your editor to type it for you when you press a certain key combo.

Overall this looks much more like a C program than a C++ one. Assuming your algorithm works as intended you can take some steps to make this more C++ idiomatic?


Replace the array with a vector and get rid of raw pointers.
Enforce const when possible.
Drop return 0 from main because the compiler will generate it for you. This refers excplicitly to the return 0 at the end of main. Exiting from main by reaching the end automatically returns 0. So adding the return statement is just duplicate code.
Prefer prefix over postfix operator.
Move the logic into a class so you don't have to pass everything on every call to nextCombination.
You can overload operator&lt;&lt; to print the current line.
Eliminate magic numbers.


Keeping all this in mind, the rewrite could look something like this:

#include &lt;iostream&gt;
#include &lt;iterator&gt;
#include &lt;vector&gt;

class Combinatorics {
public:
    Combinatorics(std::vector&lt;int&gt; const&amp; v, int const&amp; n, int const&amp; r)
        : elements{v}
        , n{n}
        , r{r}
    {}

    friend std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, Combinatorics const&amp; obj);

    bool next() {
        int lastNotEqualOffset = r - 1;
        while (elements[lastNotEqualOffset] == n - r + (lastNotEqualOffset + 1)) {
            --lastNotEqualOffset;
        }
        if (lastNotEqualOffset &lt; 0) {
            return false;
        }
        ++elements[lastNotEqualOffset];
        for (int i = lastNotEqualOffset + 1; i &lt; r; ++i) {
            elements[i] = elements[lastNotEqualOffset] + (i - lastNotEqualOffset);
        }
        return true;
    }

private:
    std::vector&lt;int&gt; elements;
    int n;
    int r;
};

std::ostream&amp; operator&lt;&lt;(std::ostream&amp; os, Combinatorics const&amp; obj) {
    std::copy(obj.elements.begin(), obj.elements.end(), std::ostream_iterator&lt;int&gt;(os, " "));
    return os;
}

int main() {
    constexpr int n = 7;
    constexpr int r = 3;
    int total = 0;
    Combinatorics combinatorics{{1, 2, 3}, n, r};
    do {
        std::cout &lt;&lt; combinatorics &lt;&lt; "\n";
        ++total;
    } while (combinatorics.next());
    std::cout &lt;&lt; total &lt;&lt; "\n";
}


You simply pass all your data into the constructor upfront and then call next until every repetition has been done. (I kept n and r because they are used in the math context, normally you should try to avoid short variable names like that).