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;
}