I'm having a problem in push function.Variable inside array subscript(top) is being initialized with r-value(item).
1.when I do the same operation with top=0 it works fine.
2.problem occurs only when top=-1.
3.I have printed the top value each time to ease up debugging.
class stack
{
int top;
public:
int arr[500];
stack()
{
top=-1;
}
void push(int);
};
void stack::push(int item)
{
arr[top++]=item; //problem here
cout<<"\n"<<item<<"pushed in stack";
cout<<"\n top value is"<<top;
}
int main()
{
stack s;
s.push(12);
s.push(19);
s.push(31);
return 0;
}
I expected the output as"12 pushed in stack and top value =-1, but the actual output is 12 pushed in stack and top value is 12
Aucun commentaire:
Enregistrer un commentaire