c++ - I am not able to run this code on Code::blocks IDE but works fine online...!!can anyone tell me what modifications i should to get it working on IDE? -
by using typdef have defines stack, code blocks gives me error while call object.
#include <iostream> using namespace std; struct stack { int data[20]; int top; };
in class, stack *s
called , when run program gives error "s" maybe used uninitialised. whereas have initilaised using constructor.
class stackop { public: stack *s; stackop() { s->top= -1; //constructor giving error. } bool stack_empty(); bool stack_full(); void push(); void pop(); void display(); }; bool stackop::stack_empty() { if(s->top == -1) { return 1; } else { return 0; } } bool stackop::stack_full() { if(s->top == 19) { return 1; } else { return 0; } } void stackop::push() { if(!stack_full()) { s->top=s->top + 1; cout<<"\n enter element: "; cin>>s->data[s->top]; } else { cout<<"\n stack full"; } } void stackop::pop() { if(!stack_empty()) { cout<<"\n value deleted or poped "<<s->data[s->top]; s->top=s->top-1; } else { cout<<"\n stack empty"; } } void stackop::display() { cout<<"\n stack follows..."; for(int i=s->top; i>=0; i--) { cout<<"\n"<<s->data[i]; } }
also code runs fine on cpp.sh terminates terminal of code blocks returning garbage value.
int main() { int y; char ch; stackop s1; { cout<<"\n 1. push."; cout<<"\n 2. pop."; cout<<"\n 3. display stack."; cout<<"\n\n enter choice :: "; cin>>y; switch(y) { case 1: s1.push(); break; case 2: s1.pop(); break; case 3: s1.display(); break; } cout<<"\n want continue? : "; cin>>ch; } while(ch=='y'); return 0; }
make small change in program work fine
stack *s = new stack;
by doing above change ,program worked in code block ide
Comments
Post a Comment