Trying to learn recursion. Problem asks to use recursion on non-negative number to insert commas. Ex: 20131 as 20,131. My program adds in an extra comma at end. Any help in pushing me in the correct direction to fix this would be appreciated.
Tried to use a 'count' to determine end of number but can not figure it out.
using namespace std;
void commaInsert(int v, int &count)
if (v <= 0)
cout<<"Count equals: "<<count<<endl;
return; //base case
int input = 546789301, count = 0;
Sorry about text screw up. Original question:
Trying to learn recursion. Problem asks to use recursion on non-negative number to insert commas. Ex: 20131 as 20,131.
My program adds an extra comma at end. Any help in pushing me in the right direction would be appreciated.
Tried to use a 'count' to determine end of number, but can not get it working.
I know why the comma is being output. I don't know how to stop it from being outputted on the last iteration. Maybe my recursion function can not be corrected with the current design? Recursion is difficult and I am looking for some pseudo code ideas to correct my current design. Thanks in advance for your time.
void outNumber(int num)
if (num < 1000) // Last segment
printf("%3d", num); // output it and return
part = num % 1000;
num /= 1000;
if (num > 0) outNumber(num);
int main(int argc, char *argv)
int num = 23456; // Default this number
if (argc > 1) // Allow a number from the command line
num = atoi(argv);
outNumber(num); // Call function