Error in Stacks program

I am getting a run-time error in my program.
Here is the code:



The program shows unknown values when you select the 3rd option. Please help.
Thanks in advance.

You have not initialized next pointer of the last element in the stack to NULL. Just write a constructor in the st class:
st() {
next = NULL;
That would solve your problem.

Also you are not releasing the memory allocated by new in option 2, causing memory leaks. In case 2, before the break statement, write this statement: delete ptr;

If you want an implementation of a stack, you should consider using std::stack class in STL. Here’s a reference to STL stack:

Thanks a lot.