I am learning stacks, and I am trying to get this program to work. I have to be able to add large integers such as 9999999999999999999999999 by pushing all the numbers on the stack and then adding them up. Currently it works with small numbers but large numbers it crashes. I know it crashes because int can not take such a large number but what can I do to fix that problem? Please Help!!

usingnamespace std;

int _tmain(int argc, _TCHAR* argv[])
stack <int, vector<int> > MyStack;
int total = 1;
unsigned int number;
cout<<"============== STACK DEMO ====================="<<endl<<endl;
//Gets input from user interface
cout<<"ENTER IN INTEGER (-1 to stop) : ";
cout<<"ENTER IN INTEGER (-1 to stop) : ";
}while (number != -1);
system ("cls");
//Adds the stack up
int add;
add = MyStack.top();
for (int i = 2; i < (total); i++)
cout <<endl<<add<<" + "<<MyStack.top();
add = add + MyStack.top();
cout<<" = "<<add<<endl;
//Outputs the total and then empties the stack
cout<<endl<<endl<<endl<<"\t \t TOTAL = "<<add<<endl<<endl;
system ("pause");
return 0;

you need to have a user defined type for performing arithmetic operations (atleast a plus) on integers with large precision. either write a big_integer class or use a library
eg:class bigint from the LiDIA library (http://www.cdc.informatik.tu-darmstadt.de/TI/LiDIA/)

I am not sure what you mean by this:

you need to have a user defined type for performing arithmetic operations (atleast a plus) on integers with large precision. either write a big_integer class or use a library

I am still new to C++ and to be very confused.

C++ is extremely limited in the values you can use. For example, the int data type is only guaranteed to hold the values -32,767 to 32,767. A large integer like 9999999999999999999999999 can't be stored in a single variable of any type. So you get around it in one of two ways.

The first way is manually. You store pieces of the value in multiple variables and write functions that handle the math between those variables. This is tough, and I don't recommend it if you're not confident in your C++ abilities.

The second way is by using a library that does the manual stuff for you. This one for instance. The problem with this solution is that you have to learn a library and now you're bound to something that isn't standard.

Now, from the looks of your problem, I'd say you've been given an assignment to manually perform addition on large integers. But it's hard to be sure because you've been rather vague in the requirements. Can you describe your assignment in a little more detail? There's a possibility that you simply asked the wrong question and that's why the answers are confusing you.

Assignment was write a program to add large integers by adding two numbers and then repeatedly add the next number with the result of the previous addition using stacks

Do you mean add the digits together? Or are you really going to get multiple huge numbers that need to be added together? Show us some sample input and the expected output.

Something like this
999999999999999999999999+9999999999999999+999999999999999999999+9999999999999999999999 = 11000999999999999997

the addition in my example is not correct becuase I just put numbers in to show what I mean.

So do you know how to perform addition on paper?
You know, start at the 'units' column of both numbers, add them together and if necessary propagate a 'carry' into the addition of the 'tens' column.

When you understand that bit, then think about how you would do the same with a stack - pop the units off two input stacks, add them and push the result onto another stack.

From there, writing the code should be pretty easy.

THe addition is not the hard part, it is bringing in such a large integer

Member Avatar for iamthwee

>it is bringing in such a large integer
I honestly don't see what the problem is. Have you even bothered to google for how it might be accomplished? Have you even read the previous posts? Telling you to first plan how you would do it on paper then just apply it to your model/ program?

> it is bringing in such a large integer
What's so hard about reading a string, then extracting each digit, and pushing the value of the digit onto a stack?

What I don't know is extracting each digit from a string. I have googled it but did not find what I was looking for.

Read the number as a string and then index it to get each digit. You're making this way harder than it needs to be.

#include <iostream.h>
class HugeInteger
        int input();      
    void output(int);
     void output1(int);
    HugeInteger add(HugeInteger,int);
    HugeInteger subtract(HugeInteger,int);
    void append(int,int);


    int line[41];
    int i,n,bigNumber[40],big[40];      

};      //End class HugeInteger
int HugeInteger::input()

Be a part of the DaniWeb community

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