segment fault in my stack program

I created a stack program but when i compile this program on g++ compiler its giving a error "segment fault"

code is here:


#include<iostream>
using namespace std;
#define MAX 5

class stack{


public:

int top;
int st[MAX];

stack(); //constructor
void push(int n); //for adding value
int pop(int n); //for deleting value


};


stack::stack()
{
top==-1;
}

void stack:: push(int n)
{
if(top>=MAX-1)
{
cout<<"overflow";
}
else
{
top++;
st[top]=n;
}
}

int stack:: pop(int n)
{
if(top<0)
{
cout<<"underflow";
}
else
{
n=st[top];
top--;
return n;
}
}



int main()
{
stack s;
int a;
cout<<"enter no ";
cin>>a;

s.push(a);

cout<<a;

return 0;
}


please help !!
Welcome to the forum. Please use the code format tag to format your code,

The error is here, you've use comparison instead of assignment, so top remains uninitialised.
1
2
3
4
stack::stack()
{
    top==-1;
}
thnx a lot kbw
Topic archived. No new replies allowed.