You can use an algorithm. I would write a function that does the reformatting and returns the formatted number. Then have the calling function display the formatted number. You don't want the display in the function because the function would now be doing more than one thing and this is a design no-no.
The function should have two arguments, the number and a pointer to the formatted result. This keeps the function out of the memory allocation business.
The algorithm is:
Is the number 0. If yes you are done.
I usually see this done when formatting money values $123,456.78. To do this your original number must be in pennies.
int data = 12345678;
cout << '$';
cout << data / 100000 << ','; //this is 123
data = data %100000; //removes the 100,000's. Remainder is 45678
cout << data / 100; //this is 456
data = data % 100; //removes the 100's. Remainder is 78. Anything less then 100 is pennies
cout << '.';
cout << data << endl;
You should now have $123,456.78.
Does this help?