for eg. 432567

sum =4+3+2+5+6+7=27

i 'm using turboc++ compiler from borland and i 'm runing my program on DOS.

Expand|Select|Wrap|Line Numbers

- #include<iostream.h>
- #include<conio.h>
- long poweroften(int); //to calculate power of 10
- void main()
- {
- clrscr();
- unsigned long number=241526; //to find the sum of digits as:
- //2+4+1+5+2+6=20
- unsigned long numstore=number; //to keep backup of number
- unsigned long *quo =new unsigned long[]; //to calculate quotient
- unsigned long *rem =new unsigned long[]; //to calc. remainder
- int i,sum=0;
- cout<<"\n the no. is: "<<number; //to display original number
- /*cout<<"\n quo is pointing to: "<<quo;
- cout<<"\n rem is: "<<rem;*/
- for(i=0;number!=0;i++)
- {
- rem[i]=number%poweroften(i+1); //finding remainder
- number-=rem[i]; //substracting the rem from number
- quo[i] = rem[i]/poweroften(i); //then calculating quotient
- cout<<"\n rem is: "<<(rem+i)<<"\n and quo is: "<<quo+i;
- //cout<<"\n rem is: rem["<<i<<"]"<<rem[i]<<"\t and QUO["<<i<<"]"<<quo[i];
- cout<<"\n quo is pointing to: "<<quo+i; //to get the address of quo elements
- cout<<"\n rem is pointing to: "<<rem+i; //to get the address of rem elements
- getch();
- }
- cout<<"\n and quo[0] is: "<<quo[0]; //it should store 6(the first remainder) but it's value is: 1000 that is rem[2]'s value
- // remaining array values are not changed
- for(int j=0;j<i;j++)
- {
- cout<<"\n in the sum loop quo is pointing to: "<<quo;
- cout<<"\n adding "<<quo[j]; // to calculate the sum of quo[] array elements
- sum+=quo[j];
- }
- cout<<"\n sum of number: "<<numstore<<" is: "<<sum;//sum is wrong since quo[0] is modified by the compiler
- }
- long poweroften(int i)
- {
- unsigned long prod=1;
- for(int j=0;j<i;j++)
- {
- prod*=10;
- }
- return prod;
- }

and if the number is large(for eg- 2146898787) quo[1] and quo[2] are also overlapped by rem[3] and rem[4].

i don't know why it's happening.