Programming Forums
User Name Password Register
 

RSS Feed
FORUM INDEX | TODAY'S POSTS | UNANSWERED THREADS | ADVANCED SEARCH

 
 
Thread Tools Display Modes
Prev Previous Post in Thread   Next Post in Thread Next
Old May 8th, 2006, 1:05 AM   #1
bl00dninja
Programming Guru
 
bl00dninja's Avatar
 
Join Date: Oct 2004
Location: namespace std
Posts: 1,246
Rep Power: 5 bl00dninja is on a distinguished road
stack question

am i creating a dangling pointer by NOT deleting the array associated with the stackClassDriver objects i'm making?

note: MaxStack is a constant int declared globally (for debugging and shit)

here is the class declaration:
template< class stackType, int MaxStack >
  class Stack
  {
	private:
		stackType stackItems[ MaxStack ];
		int top;

	public:
		Stack()		{ top = -1; }//set top to reside right before the first element when empty
		void push( stackType );
		void pop();
		void print();
  };

here is the definition:
//push an element onto the stack
template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::push(stackType pushVal) 
  {
	  //make sure stack is not full
	  if (top < MaxStack - 1)
	  { 
	      stackItems[++top] = pushVal;//increment top then add stackItem
		  cout<<"\n\n"<<endl;
	  }
	  else
		  cout<<"Error, stack bound exceeded (greater than 100 items in stack)\n\n"<<endl;
  }

  //pop an stackItem off of the stack and display what was just popped
  template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::pop() 
  {
	  cout<<"\n\n\n\n"<<endl;
	  if (top > -1)
	  {
		  cout<<"\""<<stackItems[top--]<<"\" was popped off the stack\n\n"<<endl;//display then pop
	  }
	  else
	  {
		  cout<<"Error, bottom of stack reached\n\n"<<endl;
	  }
  }

  //print elements in the stack from top to bottom
  template< class stackType, int MaxStack >
  void Stack< stackType, MaxStack >::print() 
  {
	  //temp var to keep from changing the stack just to print
	  cout<<"\n\n\n\n"<<endl;
	  int printerPointer = top;
	  while (printerPointer > -1)
	  {
		  cout<<stackItems[printerPointer]<<endl;
		  printerPointer--;
	  }
	  cout<<"\n\n\n\n"<<endl;
  }

here is the driver:
note that it repeats for whatever class type they want to make
int main() 
{
	//Stack<int, MaxStack>* stackClassDriver;
	//int stackItem;
	while (1)//begin main while
	{
		int mainMenuChoice;  //variable for stack type

		cout<<"which stack would you like to create?"<<endl;
		cout<<"1:   int\t2:   long\n3:   float\t4:   char"<<endl;
		cout<<"5:   quit"<<endl;
		cout<<"(choose 1-5)\n\n"<<endl;

		cin>>mainMenuChoice;		

		if (mainMenuChoice == 1)//begin int stack stackClassDriver
		{
			Stack<int, MaxStack>* stackClassDriver = new Stack<int, MaxStack>;
			while (1)
			{
				int stackItem;
				int stackOperation;

				cout<<"1:\tpush\t2:\tpop"<<endl;
				cout<<"3:\tprint\t4:\tmain menu"<<endl;
				cout<<"(choose 1-4)\n\n"<<endl;
				cin>>stackOperation;

				if(stackOperation ==1)
				{    
					cout << "Enter stackItem to push: ";
					cin >> stackItem;
    
					stackClassDriver->push(stackItem);
				}

				else if(stackOperation == 2)
				{
					stackClassDriver->pop();
				}

				else if(stackOperation == 3)
				{
					stackClassDriver->print();
				}

				else if(stackOperation == 4)
				{
					delete stackClassDriver;
					stackClassDriver = 0;
					break;
				}

				else
				{
					cout<<"invalid selection...\n\n"<<endl;
					continue;
				}
			}//end while
		}//end int stack stackClassDriver

		else if (mainMenuChoice == 2)//begin long stack stackClassDriver
		{
			Stack<long, MaxStack>* stackClassDriver = new Stack<long, MaxStack>;
			while (1)
			{
				long stackItem;
				int stackOperation;

				cout<<"1:\tpush\t2:\tpop"<<endl;
				cout<<"3:\tprint\t4:\tmain menu"<<endl;
				cout<<"(choose 1-4)\n\n"<<endl;
				cin>>stackOperation;

				if(stackOperation ==1)
				{    
					cout << "Enter stackItem to push: ";
					cin >> stackItem;
    
					stackClassDriver->push(stackItem);
				}

				else if(stackOperation == 2)
				{
					stackClassDriver->pop();
				}

				else if(stackOperation == 3)
				{
					stackClassDriver->print();
				}

				else if(stackOperation == 4)
				{
					delete stackClassDriver;
					stackClassDriver = 0;
					break;
				}

				else
				{
					cout<<"invalid selection...\n\n"<<endl;
					continue;
				}
			}//end while
		}//end long stack stackClassDriver

		else if (mainMenuChoice ==3)//begin float stack stackClassDriver
		{
			Stack<float, MaxStack>* stackClassDriver = new Stack<float, MaxStack>;
			while (1)
			{
				float stackItem;
				int stackOperation;

				cout<<"1:\tpush\t2:\tpop"<<endl;
				cout<<"3:\tprint\t4:\tmain menu"<<endl;
				cout<<"(choose 1-4)\n\n"<<endl;
				cin>>stackOperation;

				if(stackOperation ==1)
				{    
					cout << "Enter stackItem to push: ";
					cin >> stackItem;
    
					stackClassDriver->push(stackItem);
				}

				else if(stackOperation == 2)
				{
					stackClassDriver->pop();
				}

				else if(stackOperation == 3)
				{
					stackClassDriver->print();
				}

				else if(stackOperation == 4)
				{
					delete stackClassDriver;
					stackClassDriver = 0;
					break;
				}

				else
					cout<<"invalid selection...\n\n<<endl";
					continue;
			}//end while
		}//end float stack stackClassDriver

		else if (mainMenuChoice == 4)//begin char stack stackClassDriver
		{
			Stack<char, MaxStack>* stackClassDriver = new Stack<char, MaxStack>;
			while (1)
			{
				char stackItem;
				int stackOperation;

				cout<<"1:\tpush\t2:\tpop"<<endl;
				cout<<"3:\tprint\t4:\tmain menu"<<endl;
				cout<<"(choose 1-4)\n\n"<<endl;
				cin>>stackOperation;

				if(stackOperation ==1)
				{    
					cout << "Enter stackItem to push: ";
					cin >> stackItem;
    
					stackClassDriver->push(stackItem);
				}

				else if(stackOperation == 2)
				{
					stackClassDriver->pop();
				}

				else if(stackOperation == 3)
				{
					stackClassDriver->print();
				}

				else if(stackOperation == 4)
				{
					delete stackClassDriver;
					stackClassDriver = 0;
					break;
				}

				else
					cout<<"invalid selection...\n\n<<endl";
					continue;
			}//end while
		}//end char stack stackClassDriver*/

		else if (mainMenuChoice == 5)//quit app
		{
			break;
		}

		else//detect invalid mainMenuChoice
		{
			cout<<"invalid selection...\n\n"<<endl;
			continue;
	    }
	}//end main while

    return 0;
} //end main
__________________
i put on my robe and wizard hat...

Have you ever heard of Plato, Aristotle, Socrates?...Morons.
bl00dninja is offline   Reply With Quote
 

Bookmarks

« Previous Thread in Forum | Next Thread in Forum »

Currently Active Users Viewing This Thread: 1 (0 members and 1 guests)
 
Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump




DaniWeb IT Discussion Community
All times are GMT -5. The time now is 8:15 PM.

Powered by vBulletin® Version 3.7.0, Copyright ©2000 - 2008, Jelsoft Enterprises Ltd.
Copyright ©2007 DaniWeb® LLC