vector<int> vec(unsigned int n)
{
vector<int> num;
while (n != 0)
{
num.push_back(n%10);
vec(n / 10);
}
return num;
}
This is a function to parse a user entered int into digits. I'm making a recursive call to the function which returns a vector. I check till the value of the number becomes zero. But when I run it, it's entering into a infinite loop.
What can be the problem?
You basically have 2 loops right there 1 because of the recursivity and one because of the while.
The recursive call is correct but you should not put the condition into a while. There's where the infinite looping appears. In the while you are checking for n to be != 0, but n is not modified in that body. You should have: