i have writen the program but i cant seem to print result. the program builds a stack of numbers, then it adds,subs,mult, or divides all the numbers. for example 4 numbers are put in 1,2,3,4 it will ask what you wanna do . the answer should be 10 but instead it does a loop and just prints a and b (the numbers that i am poping). so it will give me 43 then 21. if someone can plz just explain what im doin wrong. im a student and i would really be thankful for the help :).

# include <stack>
# include <iostream>
# include <fstream>
using namespace std;
stack <int> operands;           
        int moredata= 20,number,a,b;
        char command;
int main()
	{
	   
	   
	    
	 
		while(moredata>0) 
		{	 
		cout << "Please enter the number: "; //read number.
	                cin >> number; 
		cout<<" to end stack put a zero to add put 1: ";// to end the stack or not
		cin>> moredata;
                                operands.push(number);// push into stack

		}
                           
	   while(moredata==0)
	    {   
			    cout<<" what do you want to with the numbers "<<endl;
			    cout<<" you can +,-,/,*:  ";// command
			    a=operands.top();// pop first number
	                                    operands.pop();
			    b=operands.top();// pop second number
			    operands.pop();
		                    cin>>a>>b>> command ;
	                                    if (command =='+')
			   operands.push(a+b);
				
			   else if (command =='-')
			   operands.push(a-b);
							
	                                   else if (command =='*')
			   operands.push(a*b);
						
	                                   else if (command =='/')
			   operands.push(a/b);
							
							
							
							
	    }
	 
	 return 0;   
	}

Edited 6 Years Ago by asweetroxxi: comments

All you want on line 32 is the command from the user.

To look a little further, how are you going to know how many operands remain on the stack and whether it's valid to call another command. I'd suggest you check to make sure there is more than 1 operand in the stack before you proceed through another loop through. You could change the condition of the while() controlling the loop to check if the size of the loop is more than one or you could check internal to the loop before you do the popping and user command input stuff. When the second loop ends. top() is the answer.

so i was able to understand what you were say so i was able to get it to print but it only does 2 numbers at a time and not the whole stack. so it too repetitve.
how can i do what you are saying. i would really love the help.

cout<<" the top is:"<<a<<command<<b<<"="<<operands.top()<<endl;

this was the ending for it to print. but like i said i dont want to have to onstantly do it over for every two numbers.

Do you mean something like this:

double temp;
char space = ' ';

//process stack until only one operand remains in stack
while(operands.size() > 1)	    
{   
   //get first two operands off stack	
   a=operands.top();
   operands.pop();
   b=operands.top();
   operands.pop();

   //get user selected command    
   cout<<" what do you want to with the numbers "<<endl;
   cout<<" you can select +, -, /, or *:  ";// possible commands
   cin >> command ;	

    //process command using available operands
    if (command == '+')
       temp = a + b;
    else if (command == '-')
       temp = a-b;
    else if (command =='*')
        temp = a*b;
    else if (command =='/')
        temp = a/b; 

     //push value of temp onto the stack for use in next step
    operands.push(temp); 

    //display the mathematical operation that just took place
    cout << a <<  space  << command << space << b << " = " << temp << endl; 
}
This article has been dead for over six months. Start a new discussion instead.