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;
}