This is your bin
function reduced to the bare minimum:
string bin(int n){
while(n!=0)
{
n%=2;
}
return {};
}
If n
is even you will set it to 0
on the first iteration, otherwise you set it to 1
and never change it afterwards (1%2==1
). Hence you have a endless loop. I won’t spoil you the “fun” of completing the exercise, so I will just point you to using a debugger. If you step trough your code line by line you could have observed how n
never changes and why the loop wont stop.
PS: (spoiler-alert) you might want to take a look at std::bitset
(end of spoiler)
solved Why this code doesn’t allow me to receive the whole array?