can any one please identify the mistake in my code???
my code is::

#include<iostream>

using namespace std;

class Stack{
    int *arr;
    int size;
    int top;
    
    public:
        Stack(){arr = NULL; top = 0;}
        Stack(int sz){
                arr = new(nothrow) int[sz];
                if(!arr) exit(1);
                
                size  = sz; 
                top = 0;
        }
        ~Stack(){delete []arr;}
        int pop();
        void push(int i);
        bool isEmpty();
        bool isFull();
};

int Stack::pop()
{
        top--;
        return arr[top];
}

void Stack::push(int x)
{ 
        arr[top] = x;
        top++;
}

bool Stack::isFull()
{
    return (top<size) ? false : true;
}

bool Stack::isEmpty()
{
    return (top>0) ? false : true;
}

void pushElem(Stack &s, int i)
{
    if(!s.isFull())
    {
        cout << "Pushing " << i << " into stack" << endl;
        s.push(i);
    }
    else{
        cout << "Error: Stack is full" << endl;
    }
}

int popElem(Stack &s)
{
    if(!s.isEmpty())
    {   
        int ret = s.pop();
        cout << "Popping " << ret << " from stack" << endl;
        return ret;
    }
    else
    {
     cout << "Error: Stack is empty" << endl;
     return -1;
    }
}

int main()
{
    cout << "Stack Example" << endl;
    Stack s(10);
    pushElem(s, 1);
    pushElem(s, 2);

    popElem(s);
    popElem(s);
    
    
    for ( int i = 0; i<11; i++)
        pushElem(s, i*10);
    
    for ( int i = 0; i<11; i++)
        popElem(s);
    
}

when i compiled it the following error is exhibited:
$g++ -o try tryone.cpp
tryone.cpp: In constructor ‘Stack::Stack(int)’:
tryone.cpp:14: error: ‘exit’ was not declared in this scope

Recommended Answers

All 3 Replies

Mistake 1 : Put your codes inside code tags or else no one will care to look at it.

Mistake 2 : exit() is a function and it is defined inside cstdlib header with respect to c++ and you haven't included it in the headers so the error not defined in this scope.

Mistake 1 : Put your codes inside code tags or else no one will care to look at it.

Mistake 2 : exit() is a function and it is defined inside cstdlib header with respect to c++ and you haven't included it in the headers so the error not defined in this scope.

Very much Thanks .I forgot to see that..

The cstdlib header contains the definition for the exit function as csurfer said, however up until recently this header was generally included by dependency - often unnecessarily - by other headers. So you are likely to see tutorials and books that rely on this - and other - header dependencies that have now been cleaned up.

For example you may just #include <iostream> to use the exit function in gcc 3.4 because iostream includes cstdlib however in gcc 4.3 this dependency has been removed so you need to #include <cstdlib> to utilise it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.