Stack Operation Program

It doesnt work the way I want to, what changes should I make?

Page 1 of 1

8 Replies - 4322 Views - Last Post: 19 September 2010 - 09:27 AM Rate Topic: -----

#1 RuneKnight XD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-August 10

Stack Operation Program

Posted 16 September 2010 - 09:05 AM

Err.. here is the code:

#include<iostream>
#include<stdlib.h>
#include<stack>

using namespace std;
#define Stacksize 10

int main()

{
	int choice;
	char ans;
	int a[5], x;

	stack<int> Reggie;


again:
		

		cout<<"Please Enter Desired Stack Operation: " <<endl <<endl;
		cout<<"[1]Push" <<endl <<endl;
		cout<<"[2]Pop" <<endl <<endl;
		cout<<"[3]Empty" <<endl <<endl;
		cout<<"[4]Top" <<endl <<endl;
		cout<<"[5]Quit" <<endl <<endl;
		cout<<"\t" <<"Enter Choice: ";
		cin>>choice;

			
	switch(choice)
		{
		//[1]Push
	case 1:

		for(x=0 ; x; x++) Reggie.push(x);
		{
			cout<<"Enter an Element: ";
			cin>>a[x];
			Reggie.push(a[x]);
			
			system("cls");

		if(a[x]>=1) 
		{

		while (!Reggie.empty())
		{
			cout<<"\t" <<"The Element [" <<Reggie.top() <<"] has been pushed..." <<endl <<endl;
			Reggie.pop();
		}
	
			goto again;
		}

		else if(a[x] == -1 || a[x] == 0) 
		{
			cout<<"Stack Underflow!!" <<endl;
			goto again;
		}

		else 
		{
			return 0;
		}

		}

		break;

		


		//[2]Pop
	case 2:

		Reggie.push(x);

		 system("cls");

		while (!Reggie.empty())
		{
		   cout<<"The Elements " << "[" << Reggie.top() <<"]" <<" has been popped" <<endl <<endl;
		   Reggie.pop();
		   goto again;
		}


		break;


		//[3]Empty
	case 3:
		//<if statements here>

		break;


		//[4]Top
	case 4:
	    //<if statements here>

		break;


		//[5]Quit
	case 5:
		
		cout<<"\tWanna quit? <Y/N> ";
		cin>>ans;

	if (ans == 'Y' || ans == 'y')
	{
		system("cls");
		cout<<"\t\t\t\tGoodbye~!!" <<endl;
	}

	else if (ans == 'N' || ans == 'n')
	{
		system("cls");
		goto again;
	}


		break;	


	default:
		system("cls");
		}

	
	return 0;	
		
}


So.. my problem there is.. When I choose the Stack operation 1 (which is Push), it still shows in the output that it "Pushes" the number that I entered... Now my problem is, when I try to pop the number that I pushed... it only pops the number 0, why is that? I mean, what part of the code do I need to change?

And, how am I gonna make the number that I pushed be placed into the Memory, so that when I Pop it, it will show all of the number that I entered starting from the Last one up to the first one?

So.. thanks so much for the help in advance.

EDIT:
Oh, I haven't done the 3 and 4 Stack Operation Codes yet, because I cant get past the 1 and 2 so...

This post has been edited by RuneKnight XD: 16 September 2010 - 09:07 AM


Is This A Good Question/Topic? 0
  • +

Replies To: Stack Operation Program

#2 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Stack Operation Program

Posted 16 September 2010 - 09:40 AM

I'm not exactly sure what the assignment is, but it seems like the program is supposed to only perform stack operatins (option one will push an element, option 2 will pop the top of the stack, ect). If that is the case, why do you pop all the elements when the user chooses option 1 which is push? It seems like you do not quite understand what a stack is, because most of your program does not make sense. You can think of a stack as an array, but with limited access through the functions push, pop, and top. When you push an item, it is stored in the stack. You don't need to store it in an array as well. The program should be fairly simple. For example, push should look something like.

case 1:
     cout<<"Enter an Element: ";
     cin>>input; //no need for an array, just a variable.
     Reggie.push(input);
     cout<<"\t" <<"The Element [" <<Reggie.top() <<"] has been pushed..." <<endl <<endl;
     goto again;  //I personally dont have a problem with it, but a lot of people complain about goto's.  You may want to replace "again" with a while loop



Was This Post Helpful? 2
  • +
  • -

#3 n8wxs  Icon User is offline

  • --... ...-- -.. . -. ---.. .-- -..- ...
  • member icon

Reputation: 972
  • View blog
  • Posts: 3,878
  • Joined: 07-January 08

Re: Stack Operation Program

Posted 16 September 2010 - 09:42 AM

Why are you pushing 'x' onto the Reggie stack???:

Why are you using an array - a[]??

...
case 1:
    for(x=0 ; x; x++) // what is 'x;'???
        Reggie.push(x);
//// end of for() loop

    {
        cout<<"Enter an Element: ";
        cin>>a[x];
        Reggie.push(a[x]);

...


Was This Post Helpful? 2
  • +
  • -

#4 RuneKnight XD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-August 10

Re: Stack Operation Program

Posted 17 September 2010 - 12:45 PM

Thanks for the reply guys, now there's one more problem *Yeah, our Instructor gave us the Powerpoint file for the instructions in using stack operations... but it's very confusing and I cant follow it.*

So now my Problem is.. yeah I can now push them correctly but when I pop them, the only thing that becomes popped is the last number that I entered... So... what do you guys think is wrong with the pop?

Err here is the new code:

//Project1
#include<iostream>
#include<stdlib.h>
#include<stack>

using namespace std;
#define max 10


int main()

{
	int choice;
	char ans;
	int x;

	stack<int> Reggie;
	



again:
		

		cout<<"Please Enter Desired Stack Operation: " <<endl <<endl;
		cout<<"[1]Push" <<endl <<endl; 
		cout<<"[2]Pop" <<endl <<endl;
		cout<<"[3]Empty" <<endl <<endl;
		cout<<"[4]Top" <<endl <<endl;
		cout<<"[5]Quit" <<endl <<endl;
		cout<<"\t" <<"Enter Choice: ";
		cin>>choice;

			
	switch(choice)
		{
		//[1]Push
	case 1:

			cout<<"Enter an Element: ";
			cin>>x;
			Reggie.push(x);
			system("cls");
			
			
		if(x>=1) 
		{
			cout<<"\t" <<"The Element [" <<Reggie.top() <<"] has been pushed..." <<endl <<endl;
			Reggie.pop();
	
			goto again;
		}

		else if(x == -1) 
		{
			cout<<"Stack Underflow!!" <<endl;
			goto again;
		}

		else 
		{
			return 0;
		}


		break;

		


		//[2]Pop
	case 2:

		 system("cls");
		 Reggie.push(x);

		while (!Reggie.empty())
		{
		   cout<<"\t" <<"The Element " << "[" << Reggie.top() <<"]" <<" has been popped..." <<endl <<endl;
		   Reggie.pop();
		   goto again;
		}


		break;


		//[3]Empty
	case 3:
		//<if statements here>

		break;


		//[4]Top
	case 4:
	    //<if statements here>

		break;


		//[5]Quit
	case 5:
		
		cout<<"\tWanna quit? <Y/N> ";
		cin>>ans;

	if (ans == 'Y' || ans == 'y')
	{
		system("cls");
		cout<<"\t\t\t\tGoodbye~!!" <<endl;
	}

	else if (ans == 'N' || ans == 'n')
	{
		system("cls");
		goto again;
	}


		break;	


	default:
		system("cls");
		}

	
	return 0;	
		
}


So.. what codes in the Pop Operation should I alter so that whenever I choose the Stack Operation 2, it will show me the numbers that I entered using the Push Operation starting from the last one... and then doing it repeatedly until the stack is empty?

Err, sorry if i'm asking too much.. its just so happen that.. I cant find this Powerpoint File kind of a useful material to learn the stack operations from... Though there is internet.. I always find random codes and programming styles that I never seen before which makes things more confusing.. Anyway thanks again in Advance.
Was This Post Helpful? 0
  • +
  • -

#5 JackOfAllTrades  Icon User is offline

  • Saucy!
  • member icon

Reputation: 6039
  • View blog
  • Posts: 23,435
  • Joined: 23-August 08

Re: Stack Operation Program

Posted 17 September 2010 - 12:55 PM

Because that's how a stack works...last in, first out (LIFO). But why is there a push in your pop function?

And get rid of the gotos for the love of Pete.
Was This Post Helpful? 1
  • +
  • -

#6 Darkhack  Icon User is offline

  • D.I.C Head

Reputation: 36
  • View blog
  • Posts: 208
  • Joined: 25-November 08

Re: Stack Operation Program

Posted 17 September 2010 - 12:57 PM

View PostRuneKnight XD, on 17 September 2010 - 12:45 PM, said:

So.. what codes in the Pop Operation should I alter so that whenever I choose the Stack Operation 2, it will show me the numbers that I entered using the Push Operation starting from the last one... and then doing it repeatedly until the stack is empty?


You just need to move your goto statement outside the loop as I did in the code below.

		//[2]Pop
	case 2:

		 system("cls");
		 Reggie.push(x);

		while (!Reggie.empty())
		{
		   cout<<"\t" <<"The Element " << "[" << Reggie.top() <<"]" <<" has been popped..." <<endl <<endl;
		   Reggie.pop();
		   // remove this
		   // goto again;
		}
		// add this
		goto again;

		break;


Was This Post Helpful? 1
  • +
  • -

#7 RuneKnight XD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-August 10

Re: Stack Operation Program

Posted 17 September 2010 - 01:40 PM

Thanks so much for the reply guys, i'm gonna try it tomorrow, for now I need some sleep ahaha.

I'll tell you my progress again tomorrow, thanks a bunch.
Was This Post Helpful? 0
  • +
  • -

#8 mojo666  Icon User is offline

  • D.I.C Addict
  • member icon

Reputation: 352
  • View blog
  • Posts: 770
  • Joined: 27-June 09

Re: Stack Operation Program

Posted 17 September 2010 - 02:14 PM

Quote

You just need to move your goto statement outside the loop as I did in the code below.

Then the pop function will remove all elements from the stack which I believe is not the desired functionality of pop.

View PostRuneKnight XD, on 17 September 2010 - 11:45 AM, said:

Err, sorry if i'm asking too much.. its just so happen that.. I cant find this Powerpoint File kind of a useful material to learn the stack operations from... Though there is internet.. I always find random codes and programming styles that I never seen before which makes things more confusing.. Anyway thanks again in Advance.


Before you look at any code, you need to understand what a stack is. A stack is a collection of objects that adhere to the last in first out rule. This is achieved by allowing the programmer to only interact with the top of the stack. Stack data types immitate stacks of objects in real life. So, think of something you might stack, like paper cups. Suppose you have 3 cups numbered 1 through 3. Push adds an item to the stack, so push(1) adds cup 1 to the stack. If you then call push(2), you put cup 2 on top of cup 1. Puss(3) would then add cup 3 on top of cup 2. Top returns the top of the stack. You can only see the top cup on your stack. If you show your stack of cups to someone else, they can only see the 3 on the top cup. They do not know whether the next cup is 1 or 2. The top function is just looking at the top of the stack. Pop removes the top object of the stack. If you pop your stack of cups, you remove cup 3 from the top of the stack leaving only cup 2 on top of cup 1. Empty simply tells you if there are any elements in the stack. You still have 2 cups in the stack so it will return "false".

So with all this in mind, your code has some obvious problems. There is no reason to call pop when option 1 is selected. option 1 is supposed to push an element on the stack and nothing else. There is no reason to call push when option 2 is selected. option 2 is only supposed to pop the top element. None of the 4 functions will need for-loops or while-loops. Loops are used to repeat code, and you only need to execute a handful of instructions once in each option. The if-statements in option 1 no longer make sense since "x" does not represent the number of items in the stack. You no longer need them.

This post has been edited by mojo666: 17 September 2010 - 02:16 PM

Was This Post Helpful? 1
  • +
  • -

#9 RuneKnight XD  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 5
  • Joined: 30-August 10

Re: Stack Operation Program

Posted 19 September 2010 - 09:27 AM

Thanks for the information mojo. Err I got busy with other projects a few days ago so.. but im gonna try it now haha thanks again.

EDIT:
WHOOOO!! Ahahaha, thank you so much mojo, whew, at last it worked!!

Hmm so now Im gonna go on to doing the option 3 and 4 haha im gonna post again the outcome, thanks again.

I'm starting to get the hang of stacks hahaha.



2nd EDIT:
Ok so... I already finished the program.

//Project1
#include<iostream>
#include<stdlib.h>
#include<stack>

using namespace std;
#define max 10


int main()

{
	int choice;
	char ans;
	char letters;
	int x;

	stack<int> Reggie;
	



again:
		

		cout<<"Please Enter Desired Stack Operation: " <<endl <<endl;
		cout<<"[1]Push" <<endl <<endl; 
		cout<<"[2]Pop" <<endl <<endl;
		cout<<"[3]Empty" <<endl <<endl;
		cout<<"[4]Top" <<endl <<endl;
		cout<<"[5]Quit" <<endl <<endl;
		cout<<"\t" <<"Enter Choice: ";
		cin>>choice;

		if(choice<=5)
		{
			
	switch(choice)
		{
		//[1]Push
	case 1:

			cout<<"\tEnter an Element: ";
			cin>>x;
			Reggie.push(x);
			system("cls");
			
			
		if(x>=1) 
		{

			cout<<"\t" <<"The Element [" <<Reggie.top() <<"] has been pushed..." <<endl <<endl;
	
			goto again;

		}

		else if(x <= -1 || x==0) 
		{

			cout<<"\tStack Underflow!!" <<endl <<endl;

			goto again;

		}

                else if (x == letters)
		{

			cout<<"\tNumbers Only..." <<endl <<endl;
			goto again;

		}

		else 
		{

			cout<<"\tInvalid Input..." <<endl <<endl;

			goto again;

		}



		break;

		


		//[2]Pop

	case 2: 

			system("cls");

			while (!Reggie.empty())

			{

				cout<<"\t" <<"The Element " << "[" << Reggie.top() <<"]" <<" has been popped..." <<endl <<endl;

				Reggie.pop();

				goto again;

			}
			
			 if (Reggie.empty())
			{

				cout<<"\tThe Stack is already empty..." <<endl <<endl;

				goto again;

			}

			break;



		//[3]Empty
	case 3:

		system("cls");

  
			while(!Reggie.empty()) 
			{

				cout<<"\tThe Element [" <<Reggie.top() <<"] is now empty..." << endl <<endl;
				Reggie.pop();

				goto again;
							
			}

			if (Reggie.empty())
			{

				cout<<"\tThe Stack is already empty..." <<endl <<endl;

				goto again;

			}


		break;


		//[4]Top
	case 4:

		system("cls");

			while(!Reggie.empty())
			{
				cout<<"\tThe Element [" <<Reggie.top() <<"] is currently the top..." << endl <<endl;
				Reggie.pop();

				goto again;
			}

			if (Reggie.empty())
			{

				cout<<"\tThe Stack is already empty..." <<endl <<endl;

				goto again;

			}

		break;


		//[5]Quit
	case 5:
		
		cout<<"\tWanna quit? <Y/N> ";
		cin>>ans;

	if (ans == 'Y' || ans == 'y')
	{

		system("cls");
		cout<<"\t\t\t\tGoodbye~!!" <<endl <<endl;
		return 0;

	}

	else if (ans == 'N' || ans == 'n')
	{

		system("cls");
		goto again;

	}


		break;	


	default:
		system("cls");

		}



//==========Choice==========
		}

		else if (choice>=6)
		{

			system("cls");
			cout<<"\tInvalid Choice..." <<endl <<endl;
			goto again;

		}

		else if(choice <= -1 || choice==0) 
		{

			cout<<"\tInvalid Choice..." <<endl <<endl;

			goto again;

		}

		else if (choice == letters)
		{

			cout<<"\tNumbers Only..." <<endl <<endl;
			goto again;

		}
	
	return 0;	
		
}



But another problem is... Regarding the code where the output will be "Numbers Only...", it doesnt seem to work correctly... I mean, for example I tried to enter a Letter as an Element instead of a Number, the program will display the words "Numbers Only..." then it will return to the main menu with the influence of the goto command...

So now, it works this way:
If I entered a number as a Choice or an Element to be Pushed, the program does display the words "Numbers Only...", but when it's finally displayed, the program will lose its functionality, forcing me to ctrl+c..

So what changes in the program should I make then?

Thanks again XD

Once I finished that one, I'm already done with the entire program.

This post has been edited by RuneKnight XD: 19 September 2010 - 11:15 AM

Was This Post Helpful? 0
  • +
  • -

Page 1 of 1