So I need my main to loop back to asking for input after it provides an answer. I forgot to do this before creating my main. SOS need help putting my already built main in a loop that will continue after the answer is output.
#include <stack>
#include <iostream>
#include "calc_useful.h"
usingnamespace std;
int main()
{
// declare your stack here
stack<int> stk;
char c;
int onenum, twonum;
cout<<"Please enter your expression:\n";
c = cin.get();// priming read for the sentinel loop.
while(c != '\n')
{
if(isdigit(c)){
cin.putback(c);
cin>>onenum;
stk.push(onenum);
// stack operation here.
}
elseif(isop(c))
{
// if the stack is empty here you have an error.
if(stk.empty())
{
cout<<"Wrong Expression!"<<endl;
return 0;
}
onenum = stk.top();
stk.pop();
if(stk.empty())
{
cout<<"Wrong Expression!"<<endl;
return 0;
}
twonum = stk.top();
stk.pop();
onenum = evaluate(onenum,twonum,c);
stk.push(onenum);
// here is where you have to pop a couple of numbers,
// apply the operator to the numbers
// and then push the result back into the stack
}
c = cin.get(); // reading at the bottom of the sentinel loop
}
// this is where you get your final answer off the stack
// it should be the only number left on the stack at this point
if(stk.empty())
{
cout<<"Wrong Expression!"<<endl;
return 0;
}
onenum = stk.top();
stk.pop();
if(!stk.empty())
{
cout<<"Error. Insufficient operators for operands.\n";
return -1;
}
cout<<"The answer is: "<< onenum<<endl; return 0;
}