I have a method to convert dec to bin
QList<bool> widgetInput::decToBin(int number)
{
int remainder;
QList<bool> result;
if(number <= 1) {
result << number;
return result;
}
remainder = number%2;
decToBin(number >> 1);
result << remainder;
}
but unfortunately this method only holds one element in list .
but when I replace the “result << number” with “cout << number”
it will work.
could you please help me and let me know where is my exact problem?
regards.
On each recursive step, you are creating a new QList result; which is local to that step, then inserting the remainder into it. You don’t need recursion (and in general it should be avoided when iteration will do):
or better yet, just use a standard container: