Hello, I'm trying to push and pop a stack with a class and output the results but there seems to be something wrong with my code. Any Ideas? I'm trying to keep it simple. I'm thinking I don't need the showElements but I want some way of display the results being pushed then popped...
Code:#include<iostream> using namespace std; class stack{ private: int stackArray[10]; int top; public: void showElements(); void push(int x); int pop(); int showTop(); stack(); ~stack(); }; stack::stack(){ top = 0; stackArray[10] = 0; } stack::~stack(){ } void stack::push(int x){ stackArray[top] = x; top ++; } int stack::pop(){ int x = stackArray[top]; return x; top--; } int stack::showTop(){ return top; } void stack::showElements(){ for(int i = 0; i < top; i ++){ cout<<stackArray[i]<<endl; } } int main(){ stack _s; int val; _s.push(7); _s.push(5); _s.push(3); cout <<"Push: " << _s.showTop() << endl; _s.showElements(); val=_s.pop(); cout <<"Pop: " << val << endl; _s.showElements(); return 0; }