having coding problems commas into a string c++ recursively


the problem is its puts in too many commas when i put in high numbers and it puts them in the wrong place it works on all numbers under a million
some of the output with numbers ive put in
please enter your number: 1234567
i is 7n is 8inserting
i is 12n is 9inserting
i is 12n is 10inserting
your number is 1,,234,567
please enter your number: 123456789
i is 7n is 10inserting
i is 12n is 11inserting
i is 12n is 12inserting
your number is 123,,456,789
please enter your number: 12345678912
i is 7n is 12inserting
i is 12n is 13inserting
i is 18n is 14inserting
i is 18n is 15inserting
i is 12n is 16inserting
i is 18n is 17inserting
i is 18n is 18inserting
your number is 1,,2,,345,,678,912


hi this is my code:
#include<iostream>
#include<cstring>
using namespace std;


string comma(string& num,int n,int i,int j);

int main()
{string num;
int b,n,f,i,count;
b=0;
f=0;
i=3;
int j=0;
cout<<" program to set commas in your number"<<endl;
cout<<" please enter your number: ";
cin>>num;
reverse(num.begin(), num.end());
n=num.size();
comma(num,n,i,j);
reverse(num.begin(),num.end());
cout<<"your number is "<<num<<endl;

return 0;
}

string comma(string& num,int n,int i,int j)
{
while(i<n)// this seems to be where the problem is because it runs even though
// i is greater than n
{num.insert(i,",");
n=num.size();
j++;
i=(i+3+j);
cout<<"i is "<<i;
cout<<"n is "<<n;
cout<<"inserting"<<endl;
comma(num,n,i,j);
}
return(num);
}
closed account (3TXyhbRD)
Try changing from int to long and check with high numbers again, please use code tags ([code ] [/code]).
Last edited on
Topic archived. No new replies allowed.