stack is always empty why??(Equal Stack implementation in c++)

  algorithm, arr, c++, equals, stack
#include "stdafx.h";
#include <iostream>
#include <stack>

using namespace std;

class Stack {

public:
    int MaxStackHeight(int s1[], int s2[], int s3[], int n1, int n2, int n3)
    {
        int s1Height = 0;
        int s2Height = 0;
        int s3Height = 0;
        stack<int> st1; // declaring three stacks
        stack<int> st2;
        stack<int> st3;
        // push consolidated heights into stacks instead of individual heights
        for (int i = n1 - 1; i >= 0; i--) {
            s1Height += s1[i];
            st1.push(s1Height);
        }
        for (int i = n2 - 1; i >= 0; i--) {
            s2Height += s2[i];
            st2.push(s2Height);
        }
        for (int i = n3 - 1; i >= 0; i--) {
            s3Height += s3[i];
            st3.push(s3Height);
        }
        while (true) {
            if (st1.empty() || st2.empty() || st3.empty()) // if any of stack is empty then no comparison
                cout << "STACK IS EMPTY" << endl;

            if (s1Height == s2Height && s2Height == s3Height) // sum  of heights of all three stacks equal
                return s1Height; // then return any one of the heights
            // if height of any one greater than other two pop that one
            else if (s1Height > s2Height && s1Height > s3Height)
                st1.pop();
            else if (s2Height > s1Height && s2Height > s3Height)
                st2.pop();
            else if (s3Height > s1Height && s3Height > s2Height)
                st3.pop();
        }
    }
};

int main()
{

    Stack st;
    int n1, n2, n3, *p1, *p2, *p3;
    cin >> n1 >> n2 >> n3;
    p1 = new int[n1];
    for (int i = 0; i < n1; i++) {
        cin >> p1[i];
    }
    p2 = new int[n2];
    for (int i = 0; i < n2; i++) {
        cin >> p2[i];
    }
    p3 = new int[n3];
    for (int i = 0; i < n3; i++) {
        cin >> p3[i];
    }
    cout << st.MaxStackHeight(p1, p2, p3, n1, n2, n3);
    system("pause");
    return 0;
}

Input :

5 3 4

3 2 1 1 1

4 3 2

1 1 4 1

correct output:

5

My output :

stack is empty

=> kindly figure out any error if found in my code
=> it may be a sntax error

Source: Windows Questions C++

LEAVE A COMMENT