user10017: So, I was working on a project where I needed to get a random object from a stack, so to make sure my design was correct, I made a dummy program that does nothing but fill a stack with data, then print it out.

why in the world is nothing coming out of the stack but -1s??
Here's the stack.h file.
http://pastebin.com/vjhNLpNu

user42: You never allocate space for the stack.
The way it's written, you need to pass the stack size into the constructor, and then it's fixed for the lifetime of the object; i.e. it won't grow automatically when you call push(). You're not passing anything into the Stack constructor, so the parameter is defaulting to 0, meaning it has zero size.
The solution is to declare the stack something like this:
Stack stack(10);

(10 is just an arbitrary size -- use whatever you expect to need.)
Incidentally, there isn't an effective safeguard in your code to handle an empty stack. The result is that you're technically getting undefined behaviour at the moment when you call push() (it's writing to arbitrary memory locations).

user4143: You're not telling it how big your stack should be, and it defaults to 0 entries, which means nothing gets stored.
class Stack
{
private:
    int *p;
    int top,length;

public:
    Stack(int = 0);  <<== CONSTRUCTOR - DEFAULT SIZE IS 0
    ~Stack();

    void push(int);
    int pop();
    void display();
};

When you make your Stack object you must give it a size for the number of entries in the stack:
Stack stack(4);


10017: <p>So, I was working on a project where I needed to get a random object from a stack, so to make sure my design was correct, I made a dummy program that does nothing but fill a stack with data, then print it out.
<img src="https://i.stack.imgur.com/F6tCB.png" alt="enter image description here"></p>

<p>why in the world is nothing coming out of the stack but -1s??</p>

<p>Here's the stack.h file.</p>

<p><a href="http://pastebin.com/vjhNLpNu" rel="nofollow noreferrer">http://pastebin.com/vjhNLpNu</a></p>
 42: <p>You never allocate space for the stack.</p>

<p>The way it's written, you need to pass the stack size into the constructor, and then it's fixed for the lifetime of the object; i.e. it won't grow automatically when you call <code>push()</code>. You're not passing anything into the <code>Stack</code> constructor, so the parameter is defaulting to <code>0</code>, meaning it has zero size.</p>

<p>The solution is to declare the stack something like this:</p>

<pre><code>Stack stack(10);
</code></pre>

<p>(10 is just an arbitrary size -- use whatever you expect to need.)</p>

<p>Incidentally, there isn't an effective safeguard in your code to handle an empty stack. The result is that you're technically getting undefined behaviour at the moment when you call <code>push()</code> (it's writing to arbitrary memory locations).</p>
 4143: <p>You're not telling it how big your stack should be, and it defaults to 0 entries, which means nothing gets stored.</p>

<pre><code>class Stack
{
private:
    int *p;
    int top,length;

public:
    Stack(int = 0);  &lt;&lt;== CONSTRUCTOR - DEFAULT SIZE IS 0
    ~Stack();

    void push(int);
    int pop();
    void display();
};
</code></pre>

<p>When you make your Stack object you must give it a size for the number of entries in the stack:</p>

<pre><code>Stack stack(4);
</code></pre>
