Adding to very large numbers using stack

  addition, c++, char, stack, string

i am a novice to C++ , I was trying to write this program for adding two very large numbers using strings but the program is not working correctly and I can’t get what’s wrong with it , please help me with this.

#include<iostream>
#include<stack>
#include<string>

using namespace std;


int main() {
    stack <char> a1;
    stack<char> a2;
    stack<int> result;
    stack<int> temp;
    int carry = 0;
    string num1;
    string num2;
    cout << "Enter first number (both numbers should have equal digits)" << endl;
    getline(cin, num1);
    cout << "Enter second number" << endl;
    getline(cin, num2);

    
    for (int i = num1.size()-1; i >= 0; i--) {
        a1.push(num1[i]);
        
        a2.push(num2[i]);
        

    }

    while (!a1.empty() && !a2.empty()) {
            int element = (int)a1.top() + (int)a2.top() + carry;
         cout << element;

        if (element > 10) {
            element %= 10;
            carry = 1;
        }
        result.push(element);
        cout << result.top() << endl;
        a1.pop();
        a2.pop();


    }
    string abc;

    while (!result.empty()) {
        temp.push(result.top());
        result.pop();
        abc += temp.top();
    }

    cout << abc;


}

I know i have definitely made a logical mistake , but i can’t get it , can anyone please guide me?
the following is the output am getting

Source: Windows Questions C++

LEAVE A COMMENT