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.

input: 546789301
output: 546,789,301,

#include "stdafx.h"
#include <iostream>
using namespace std;

void commaInsert(int v, int &count)
{
	if (v <= 0) 
	{
		count++;
		cout<<"Count equals: "<<count<<endl;
		return; //base case
	}
	
	commaInsert(v/1000, count);

	cout<<(v%1000)<<((count=1)?",":"");
	return;
}

int main()
{
	int input = 546789301, count = 0;
	commaInsert(input, count);
	cout<<endl;
	return 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.
input: 546789301
output: 546,789,301,

You always output the 3 digits followed by ','. Assume you have 10. Follow the code with that value to see why the comma is output.

Walt,
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.

As you obviously know, you divide out the last 3 digits and call the function again.

When you divide out the digits and your source is 0, you have the highest value as a result (the 45 from 45,652,567)
Output it and return.

The return loop then outputs ',' followed by the precious result.

So a minor change to you function should get it. Think if-else when you get your result.

Thanks again Walt. After much time playing around with it I got it working. For any other beginner assigned this puzzle puzzler, here is a working code sample to review.

#include <iostream>
#include <iomanip>

using namespace std;

void commaInsert(unsigned v)
{
	//base case
	if (v <= 0)
	{
		return;
	}
	//main logic
	commaInsert(v/1000);
	if(v >1000)
	{
		cout<<",";
		cout<<setfill('0')<<setw(3)<<(v%1000);  //stores on stack waiting for return
	}
	else
		cout<<(v%1000);  //stores on stack waiting for return
	return;
}

int main()
{
	while (true)
	{
		unsigned input;
		cout<<"Enter an integer to process: ";
		cin >> input;
		if (input == 0)		//special case
			cout<<"0"<<endl;
		else if (input == 1000)	//special case
			cout<<"1,000"<<endl;
		else
		{
			commaInsert(input);
			cout<<endl;
		}
	}
	return 0;
}

Good job. But why is 1000 a special case? And 0?

Here's my solution, in C:

#include <stdio.h>

void outNumber(int num)
{
    int part;

    if (num < 1000)         // Last segment
    {
        printf("%3d", num); // output it and return
        return;
    }    
    part = num % 1000;      
    num /= 1000;
    if (num > 0) outNumber(num);
        
    printf(",%03d", part);
    
    return;
}


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[1]);
    
    outNumber(num);     // Call function
    
    return 0;
}

Note there are no special cases.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.