Ok I have been working on this program for some time now. The program is to take any number of any size and then add them together. After which it will print out the result. The problem I am having is that the gut of the add function seems to be working except that the numbers are being added in the wrong place.

Here is the adding function:

int largeinteger::addbignumbers(){
    
    strl1=str1.length();
    strl2=str2.length();
    int num1[strl1];
    int num2[strl2];
    int sum=0,carry=0,ascii;
    total[strl1+1];
    carry=0;
    
    for(int i=0; i<strl1; i++){
        num1[i]=0;
        num2[i]=0;
        total[i]=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;
    
    for(int x=strl1; x>=0; x--){      //backwards
    //for(int x=0; x<strl1; x++){         //forwards
        sum=num2[x]+num1[x]+carry;
        if(sum>9){
        sum=sum-carry;
        carry=1;
            }
        else{
             carry=0;
             }
        sum=sum-carry;
        total[x]=sum;
        cout<<total[x];
        }
    cout<<endl;
     
}

void largeinteger::print(){
     for(int i=0; i<strl1; i++)
         cout<<total[i];
     }

Please help me with this problem, I think I have tortured myself everyway possible getting this to work. Thanks in advance guys!

I am not advanced enough to understand most of that code, but are you trying to do more than two numbers at once? If not, i don't see why this won't work:

#include <iostream>

using namespace std;

int main()
{
int numberone, numbertwo, action;

cout<<"\nWhat is the first number?\n>";
cin>> numberone;
cin.ignore();

cout<<"\nWhat is the second number?\n>";
cin>> numbertwo;
cin.ignore();

cout<<"\nIs the first number (1) or the second number (2)?\n>";
cin>> action;
cin.ignore();

if(action == 1)
{
cout<<"\n"<< numberone <<"-"<< numbertwo <<"="<< numberone - numbertwo <<"\n";
}

if(action == 2)
{
cout<<"\n"<< numbertwo <<"-"<< numberone <<"="<< numbertwo - numberone <<"\n";
}

return 1;
}

i am sorry i just realized that it needs to be addition not subtraction. just replace any -'s with +'s. the program isn't messed up though either way. I wrote a simple calculator with this that could add subtract divide and multiply.

This article has been dead for over six months. Start a new discussion instead.