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!!
#include"stdafx.h"
#include<iostream>
#include<stack>
#include<vector>
#include<string>
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) : ";
cin>>number;
do
{
MyStack.push(number);
cout<<"ENTER IN INTEGER (-1 to stop) : ";
cin>>number;
total++;
}while (number != -1);
system ("cls");
//Adds the stack up
int add;
add = MyStack.top();
for (int i = 2; i < (total); i++)
{
MyStack.pop();
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;
MyStack.empty();
system ("pause");
return 0;
}