hi guys, im trying to complete a function where you have stack S and stack T. Both integer type and both have 4 elements in each stack which the elements are in ascending order (Top to bottom (eg..1 2 3 4 7 8)). Now i have to combine both stacks and make one union stack which combines both stacks S and T and puts that stack in ascending order.

my program only outputs 5 elements and not 8 (combining of both S and T).

Code:

stack <int> unionS(stack<int>& S, stack<int>& T)
{
stack <int> unionStack;
stack <int> a;
stack <int> b;
S.push(2);//top
S.push(4);
S.push(8);
S.push(9);//bottom
T.push(1);//top
T.push(3);
T.push(5);
T.push(7);//bottom
for(int j = S.size(); j>0; j--)
{
a.push(S.top());
S.pop();
}
for(int g = T.size(); g>0; g--)
{
b.push(T.top());
T.pop();
}
cout<<a.size()<<" ELEMENTS IN STACK a";
cout<<endl;
cout<<b.size()<<" ELEMENTS IN STACK b";
cout<<endl;
for(int x = 0; x<=(a.size())+(b.size()); x++)
{
if(a.top()<=b.top())
{
unionStack.push(a.top());
a.pop();
}
else
{
unionStack.push(b.top());
b.pop();
}
}
cout<<unionStack.size();
cout<<endl;
}

any1 know why i can only get 5 and not all 8 into the unionStack that unites both S and T and puts all the numbers in ascending order? thank u!