Well im having this problem on how to add large integers. I have used strings to take in a very large number something that can work on as many digits as you need it to. I take in 2 strings for the numbers you want to add. After which I convert the strings to int arrays by reading the ascii value and then subtracting 48 to get the actual number value.

All up to that point works fine. The part im having trouble is the for loop that adds each number at a time starting from the end of each for loop then determining the carry value and sum. Then put the number back into another array called total which will be used to print out the result.

So far i have tried so many different tweaks and remaking this code nothing works. Please help me! Im about to pull my hair out. By the way this is my first time on this forum so excuse my mistakes.

``````for(int x=strl1; x>=0; x--){
sum=num2[x]+num1[x]+carry;
carry=sum-10;
if(carry<=0){
carry=0;
sum=10%sum;
}
total[x]=sum;
}

for(int i=0; i<strl1+1; i++){
cout<<total[i];
}``````

I think this might be all you need but if not let me know ill post more of it. But this is the part that is not working right. I know im close and that there is something small that is messing me up. Thanks for the help in advance.

## All 9 Replies

You can try debug yourself by just modified your code. e.g.

``````for(int x=strl1; x>=0; x--){
cout<<"Current x: "<<endl;			//test
cout<<"=======\n";					//test
sum=num2[x]+num1[x]+carry;
cout<<"sum is "<<sum<<endl;		//test
carry=sum-10;
cout<<"carry is "<<carry<<endl;		//test
if(carry<=0){
carry=0;
sum=10%sum;
}
total[x]=sum;
cout<<"total[x] is "<<total[x]<<endl;	//test
}

for(int i=0; i<strl1+1; i++){
cout<<total[i];
}``````

Trust me I have tried that. I have been working on this for a while now and this is a complete redo of the whole thing. I have redone this bit atleast 7 times. I cant figure out what is going wrong.

Im starting to believe that the problem might lie in the for loops before in the conversion. I think the arrays arent lining up.... take a look and see waht you guys come up with. I just modified it so that i does it reads the string and puts the values in backwards into the array:

``````for(int i=strl1+1; i>=0; i--){
ascii=str1[i];
ascii=(ascii-48);
num1[i]=ascii;
}

for(int i=strl2+1; i>=0; i--){
ascii=str2[i];
ascii=(ascii-48);
num2[i]=ascii;
}``````

when the carry is less than or equal to zero , you don't have to
invoke the modulus operator ,instead if it's not [else case] you
need to invoke the modulo operator.

and

``carry = sum -10;``

is wrong !

``````if ( sum >9 )
{
carry =1; // 9+9 =18, you can't carry 2 if there's a carry then it's 1
}else{
carry =0; // do not miss this//
}``````

can you fix the code to this extend !

Man this is killing me. Here is the whole function that im trying to get to work.
I think it has to do with how the arrays are being read and where the actual numbers are that its supposed to be reading.

``````//for(int i=strl1-1; i>=0; i--){    //backwards
for(int i=0; i<=strl1; i++){
ascii=str1[i];
ascii=(ascii-48);
num1[i]=ascii;
cout<<num1[i];
}
cout<<endl;

//for(int i=strl2-1; i>=0; i--){      //backwards
for(int i=0; i<=strl2; i++){
ascii=str2[i];
ascii=(ascii-48);
num2[i]=ascii;
cout<<num2[i];
}
cout<<endl;

for(int x=0; x<strl1+1; x++){
sum=num2[x]+num1[x]+carry;
if(sum>9){
carry=1;
}
else{
carry=0;
}
sum=sum-carry;
total[x]=sum;
cout<<total[x];
}``````

can you show me what is the possible output of your progam?

Can I see your code that get the str1,str2 input?

I think you didn't put cout or didn't use debug to do testing. Or you put at wrong position...
Here is an example... with this I think you can find out your problem. Hope this help you.

``````//for(int i=strl1-1; i>=0; i--){    //backwards
for(int i=0; i<=strl1; i++){
ascii=str1[i];
ascii=(ascii-48);
num1[i]=ascii;
cout<<num1[i];

}
cout<<endl;

//for(int i=strl2-1; i>=0; i--){      //backwards
for(int i=0; i<=strl2; i++){
ascii=str2[i];
ascii=(ascii-48);
num2[i]=ascii;
cout<<num2[i];

}
cout<<endl;

for(int x=0; x<strl1+1; x++){
sum=num2[x]+num1[x]+carry;

cout<<"sum before -carry: "<<sum<<endl;
if(sum>9){
carry=1;
}
else{
carry=0;
}
sum=sum-carry;

cout<<"sum after-carry: "<<sum<<endl;
total[x]=sum;
cout<<total[x];

}``````

You aren't thinking clearly. Here is your code:

``````for(int x=0; x<strl1+1; x++){     // Why are you going forwards? Won't this add
//    the carry to the wrong value?
sum=num2[x]+num1[x]+carry;    // Where did you initialize carry? It could
//    be 7023479 because it's uninitialized
if(sum>9){
carry=1;        // True
}
else{
carry=0;        // True
}
sum=sum-carry;        // If carry is 0, why? Shouldn't this go
//    where sum > 9?
total[x]=sum;
cout<<total[x];
}``````

Ok I took a few days away from this code to help calm myself lol. Anyway thanks to you guys I made progress and now its just adding wrong. See what you guys think:

``````void largeinteger::convertstring(){
int ascii=0;
for(int i=strl1-1; i>=0; i--){    //backwards
//for(int i=0; i<strl1; i++){
ascii=str1[i];
ascii=(ascii-48);
num1[i]=ascii;
cout<<num1[i];
}
cout<<endl;

for(int i=strl2-1; i>=0; i--){      //backwards
//for(int i=0; i<strl2; i++){
ascii=str2[i];
ascii=(ascii-48);
num2[i]=ascii;
cout<<num2[i];
}
cout<<endl;

}

int sum=0,carry=0;
createarrays();
convertstring();

for(int x=3; x>=0; x--){      //backwards
//for(int x=0; x<strl1; x++){         //forwards
sum=num2[x]+num1[x]+carry;
if(sum>9){
carry=1;
sum=sum-10;
}
else{
carry=0;
}
total[x]=sum;
cout<<total[x];
}
cout<<endl;

}

void largeinteger::print(){
for(int i=0; i<3; i++){
cout<<total[i];
}
cout<<endl;
}``````
Be a part of the DaniWeb community

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