c++ - i can't get an output from this -
#include<iostream> using namespace std; class stack { int size=10; int stack[size]={0}, value=0, top; top=size; public: void push(int v) { if(top==0) cout<<"\nstack full\n"; else {--top; stack[top]=v;} } void pop() { if(top==size) cout<<"\nstack empty\n"; else {top++; stack[top]; stack[top-1]=0; } } void display() { if(top==size) cout<<"\nstack empty\n"; else { for(int i=top;i<size-1;i++) { cout<<stack[i]; } } } }; int main() { stack s; char t; int value,ch; { cout<<"\n1.push\n"; cout<<"\n2.pop\n"; cout<<"\n3.display\n"; cout<<"enter choice:\n"; cin>>ch; switch(ch) { case 1:cout<<"\nenter value pushed\n"; cin>>value; s.push(value); break; case 2:s.pop(); break; case 3:s.display(); break; default: cout<<"\nwrong choice\n"; } cout<<"\ndo u want retry\n"; cin>>t; }while(t=='y' || t=='y'); return 0; }
simplest fix errors occurring changing int size=10;
static const int size=10;
. after this, apart occurring warning stack[top];
being empty statement, there logical error in display
loop in for(int i=top;i<size-1;i++)
should either for(int i=top;i<size;i++)
or for(int i=top;i<=size-1;i++)
.
Comments
Post a Comment