Seems to me you overcomplicate the things.
IF you split your number into "begining""beginning" and "last 3 digits" inside your RecursiveCommas you have to call it recusevlyrecursively on the "begining" "beginning" and then print the "last 3 digits". The only difference will be - that 1st chunk could be less than 3 digits and has no comma before it, all other chunks - following comma have to be printed with 0 filling.
Here is thea sample implemantationimplementation of the above approach (template is just to avoid selecting specific integer type - so I'm skipping the checks of the TT intentionally):
#include <iostream>
#include <iomanip>
template<class T>
void RecursiveCommas(std::ostream& os, T n)
{
T rest = n % 1000; //"last 3 digits"
n /= 1000; //"begining"
if (n > 0) {
RecursiveCommas(os, n); //printing "begining"
//and last chunk
os << ',' << std::setfill('0') << std::setw(3) << rest;
}
else
os << rest; //first chunk of the number
}
int main()
{
RecursiveCommas(std::cout, 123456789);
std::cout << '\n';
RecursiveCommas(std::cout, 1234);
std::cout << '\n';
RecursiveCommas(std::cout, 123);
std::cout << '\n';
RecursiveCommas(std::cout, 123456789012345);
std::cout << '\n';
return 0;
}