0

i just started c++ and coded a program to push and pop in 2 stacks simultaneously inn a program....i coded it corectly but while i run the program and try to access the first stack i.e s1 it shows segmentation fault but i am able access my second stack s2 very perfectly.....help me

#include<iostream>
using namespace std;
#define max 10
class stack
{
private:
    int arr[max],top;
public:
    void init()
    {
        int top=0;
    }
    void push(int a)
    {
        arr[top++]=a;
    }
    int pop()
    {
        return arr[--top];
    }
    int isempty()
    {
        if(top==0)
            return 1;
        else
            return 0;
    }
    int isfull()
    {
        if(top==max)
            return 1;
        else
            return 0;
    }
};
int main()
{
    int a,z,cas;
    stack s1;
    stack s2;
    s1.init();
    s2.init();
    while(1)
    {
        cout<<"Enter your choice i.e. :\n";
        cout<<"1.Pushing in stack s1.\n";
        cout<<"2.Pushing in stack s2.\n";
        cout<<"3.Poping from stack s1.\n";
        cout<<"4.Poping from stack s2.\n";
        cout<<"5.To STOP.\n";
        cin>>cas;
        switch(cas)
        {
        case 1:
            cout<<"Enter the number to push in stack s1:\n";
            cin>>a;
            if(s1.isfull()==0)
                s1.push(a);
            else
                cout<<"The Stack is full.\n";
            break;
        case 2:
            cout<<"Enter the number to push in stack s2:\n";
            cin>>a;
            if(s2.isfull()==0)
                s2.push(a);
            else
                cout<<"The Stack is full.\n";
            break;
        case 3:
            if(s1.isempty()==0)
                cout<<"The number poped out is :\n"<<s1.pop()<<endl;
            else
                cout<<"The stack is empty.\n";
            break;
        case 4:
            if(s2.isempty()==0)
                cout<<"The number poped out is :\n"<<s2.pop()<<endl;
            else
                cout<<"The stack is empty.\n";
            break;
        case 5:
            cout<<"The elements in stack s1 are :\n";
            while(!s1.isempty())
                cout<<s1.pop()<<" ";
            cout<<endl;
            cout<<"The elements in stack s2 are :\n";
            while(!s2.isempty())
                cout<<s2.pop()<<" ";
            cout<<endl;
            exit(1);
        }
    }
    return 0;
}
15
  • Since you "just started C++", you should invest time in using a debugger. A debugger allows you to execute each statement one at a time (a.k.a single stepping) and see values of variables. Commented Jan 4, 2017 at 17:25
  • Since you are using C++, use the bool type with true and false values. Using 1 and 0 is an ancient practice (like around 1960's or earlier). Commented Jan 4, 2017 at 17:26
  • You can simplify some of your functions by placing the comparison into a return statement, such as return (top == max);. Commented Jan 4, 2017 at 17:27
  • Also, rename your stack as std::stack is already in use and you have allowed every symbol from the std namespace by using namespace std;. Commented Jan 4, 2017 at 17:28
  • In your push method, you need to check for stack full before pushing an element. Otherwise, you are accessing beyond the array, which causes Undefined Behavior (anything could happen). Similarly, you need to check for stack empty before popping elements. Commented Jan 4, 2017 at 17:30

1 Answer 1

2

Much more useful than a debugger is the art of reading carefully:

void init()
{
    int top=0;
}

declares a local variable top.

The member variable is left uninitialised, leading to Undefined Behaviour.
That one of the stacks appears to work is just bad luck.

Making it a proper assignment:

void init()
{
    top=0;
}

would do it, but this is C++, so you should use a constructor:

class stack
{
public:
    stack();
// ...
};

stack::stack()
    : top(0)
{

}

and then

stack s1;
stack s2;
while(1)
{
// ...
Sign up to request clarification or add additional context in comments.

1 Comment

Thanks for the help😁😁

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.