3 Replies - 252 Views - Last Post: 23 June 2013 - 10:21 PM Rate Topic: -----

#1 roberttavares92  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 9
  • Joined: 22-May 13

Program closes as soon as it compiles

Posted 20 June 2013 - 05:24 AM

I am trying to push numbers onto a stack, and push/pop them off the stack using the functions, but when i compile my program it continues to close right away, and I'm not sure why. Here is the code:

#include    <iostream>
#include    <iomanip>
using namespace std;

#include    "Stack.h"

int main()
{
	cout << "Enter the numbers you would like to put in your stack: ";
	int num, i;

	
	Stack *s=0;

	while(cin >> num)
	{
		s -> push(num);
	}

	for(i = 0; i < num ; ++i)
	{
		cout << setw(3) << i << setw(5) << s[i] << '\n';
	}

}


Stack::Stack( int maxsize )      // constructor
{
	int max;
    cout << "in Stack::Stack( " << maxsize << " )\n";
    max = maxsize;
    stack   = new int[ maxsize ];
    top     = 0;            // top of stack is at index 0
}                           // stack is empty

Stack::Stack( const Stack &s )
{
    maxsize = s.maxsize;
                            // allocate stack for left side object
    stack = new int[ maxsize ];
                            // now copy right side object to left side object
    for( int i = 0; i < maxsize; ++i )
    {
        stack[ i ] = s.stack[ i ];
    }
}


Stack::~Stack( )     // destructor
{
// a destructor should undo what a constructor does
// in this case, what is it?
}

void Stack::push( int i )   // push an int into a Stack
{
    if( ! full( ) )
    {
        cout << "push( " << i << " )\t at location "
             << top << '\n';
        stack[ top ] = i;
        ++top;              // advance to the next empty location
    }
}

int Stack::pop( )           // pop an int from a Stack
{
    if( empty( ) )
    {
        return -1;          // stack is empty; return -1
    }
    else
    {
        --top;              // next avail. position
        cout << "pop( ) " << stack[ top ]
             << " at location " << top << '\n';

        return stack[ top ];  // return item at top of the stack
    }
}

bool Stack::empty( ) const
{
    return top <= 0 ? true : false;
}
   
bool Stack::full( ) const
{
    return top >= maxsize;
}
                            // operator << for a Stack object
                            // no Stack:: scope specifier
ostream &operator <<( ostream &out, const Stack &s )
{
    out << "in operator <<( ) for Stack\n";

    for( int i = s.top - 1; i >= 0; --i )
    {
        out << setw( 3 ) << i << setw( 5 ) << s.stack[ i ] << '\n';
    }

    return out;
}





Here's the .h file:

#include    <iostream>
#include    <iomanip>
using namespace std;


class Stack
{
  public:
    Stack( int );           // constructor
    Stack( const Stack & ); // constructor
    ~Stack( );              // destructor

    void push( int );       // push an int into a Stack
    int  pop( );            // pop an int from a Stack

    bool empty( ) const;    // is the Stack empty?
    bool full( ) const;     // is the Stack full?

    int capacity( ) const;  // capacity of the stack
    int size( ) const;      // current size of the stack

    friend ostream &operator <<( ostream &, const Stack & );

  private:
    int *stack;             // pointer to local stack of ints

    int top;                // top of stack (next avail. loc.)
    int maxsize;            // max size of the stack
};






Thanks!

Is This A Good Question/Topic? 0
  • +

Replies To: Program closes as soon as it compiles

#2 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6053
  • View blog
  • Posts: 23,488
  • Joined: 23-August 08

Re: Program closes as soon as it compiles

Posted 20 June 2013 - 05:37 AM

I'm sure it's not closing as soon as it COMPILES; probably as soon as it RUNS. You are likely choosing something like "Build & Run" or "Compile & Run".

In any event, read this for solutions.
Was This Post Helpful? 0
  • +
  • -

#3 jimblumberg  Icon User is online

  • member icon


Reputation: 4007
  • View blog
  • Posts: 12,361
  • Joined: 25-December 09

Re: Program closes as soon as it compiles

Posted 20 June 2013 - 05:47 AM

Quote

but when i compile my program it continues to close right away, and I'm not sure why.


It's closing right away because it's crashing.

Why are you trying to use a pointer for your Stack?

Where do you allocate any memory for this pointer?

You also should try to increase your compiler warning level, you have several warnings you should be fixing:

Quote

warning: ‘class Stack’ has pointer data members [-Weffc++]|
warning: but does not override ‘operator=(const Stack&)’ [-Weffc++]|
In constructor ‘Stack::Stack(int)’:|
warning: declaration of ‘maxsize’ shadows a member of 'this' [-Wshadow]|
warning: variable ‘max’ set but not used [-Wunused-but-set-variable]|


Jim
Was This Post Helpful? 1
  • +
  • -

#4 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Program closes as soon as it compiles

Posted 23 June 2013 - 10:21 PM

Declaring a pointer does not allocate the memory for the pointers target. You are setting your pointer to NULL, so when you try dereferencing it, you are dereferencing a NULL pointer (google why this is bad).
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1