the following code is lethal. It compiles perfectly fine but on running gives an access violation(segmentation problem also). I don't understand the flaw in my code
and have gone MAD (literally)
over it. Could any one(i mean any) please helpme helpme helpme helpme !!!!!!!!!!
I can't figure out what this error
is please compile this code on a good compiler and then run to see yourself if you want but please tell me what
this is and how to solve it:
#include<iostream.h>
#include<stdlib.h>
int dig(long num)
{
int ret=0;
while(num)
{
num/=10;
ret++;
}
return ret;
}
void clear(char a[],int l)
{
for(int i=0;i<l;i++)
a[i]='0';
}
int check_case(char a[],int l,int len)
{
int ret=1;
int start=-1,end=0;
for(int i=0;i<l+1-len;i++)
{
if(a[i]=='1')
{
end=i;
if((end-start)>(len+1))
{ret=0;break;}
start=end;
}
}
return ret;
}
void next_case(char a[],int l,int len)
{
do{
a[l-1]++;
for(int i=l-1;i>=0;i--)
if(a[i]=='2')
{
a[i]-=2;
a[i-1]++;
}
else break;
}
while(!check_case(a,l,len));
}
int ones(char a[],int l)
{
int ret=0;
for(int i=0;i<l;i++)
if(a[i]=='1')ret++;
return ret;
}
int main()
{
int l=14345,n=152;
char *places;
char *str;
ltoa(l,str,10);
int len=dig(n);
int pl=dig(l)-1;
int max=pl;
int flag=0,start=-1,end,dist,sum=0;
char temp[5];
places=new char[pl];
clear(places,pl);
while(!flag&&(ones(places,pl)<=max))
{
next_case(places,pl,len);
for(int i=0;i<pl;i++)
{
if(places[i]=='1')
{
end=i;
dist=0;
while(start!=end)
{
start++;
temp[dist]=str[start];
dist++;
}
temp[dist]='\0';
cout<<temp<<" + ";
sum+=atoi(temp);
start=end;
}
}
if(sum==n)flag=1;
cout<<"\n";
}
return 0;
}