At least part of the problem is that you are not understanding how operators work in C++.
i<=n,completed==0 has the effect of evaluating
i <= n, discarding the result, then evaluating
completed == 0, and giving the result of that.
So the end condition of the loop is essentially
completed == 0. The relationship between
n does not affect execution of the loop.
There are certainly other problems too, but I haven’t looked further.
solved Function receiving different value than passed