1

I'm trying to code a postfix calculator, but I keep running into two issues- first: When the calculator encounters a space, it sort of just exits immediately second: when it encounters a non operator/non digit (ie- z) it doesn't display the error message that I coded.

int main()
{

stack <int> calcStack;
string exp;
char ans;
cout << "\nDo you want to use the calculator?" << endl;
cin >> ans;
while (ans == 'y')
{
    cout << "\nEnter your exp" << endl;
    cin >> exp;
    for (int i = 0; i < exp.size(); i++)
    {
        if (isspace(exp[i]))
        {

        }
        else if (isdigit(exp[i]))
        {
            int num = exp[i] - '0';
            calcStack.push(num);
        }
        else
            doOp(exp[i], calcStack);
    }

    while (!calcStack.empty())
    {
        calcStack.pop();
    }

    cout << "\nDo you want to use the calculator again?" << endl;
    cin >> ans;
}

system("pause");
return 0;
}

This is the function--

void doOp(const char & e, stack <int>& myS)
{

if (myS.size() == 2)
{
    int num1, num2, answ;
    num2 = myS.top();
    myS.pop();
    num1 = myS.top();
    myS.pop();
    if (e == '+')
        answ = num1 + num2;
    else if (e == '-')
        answ = num1 - num2;
    else if (e == '*')
        answ = num1 * num2;
    else if (e == '/')
        answ = num1 / num2;
    else if (e == '%')
        answ = num1 % num2;
    else
        cout << "\nError- Invalid operator" << endl;

    cout << "\nCalculating..." << endl << answ << endl;
    myS.push(answ);
}
else
    cout << "\nInvalid stack size- too few, or too many" << endl;
}
Jack Faber
  • 37
  • 6
  • When you used the debugger, and executed each statement one at a time, which statement was causing the issue? – Thomas Matthews Mar 22 '16 at 23:15
  • Keep in mind that when you read data from ``cin`` into an ``std:string`` object a space is seen as a seperation between two values. See http://stackoverflow.com/questions/5838711/c-cin-input-with-spaces – BrainStone Mar 22 '16 at 23:21
  • this causes the space issue-- 37* 9 - so the 3*7 will work, but not the -9. Also if I try to say 37*n+, instead of telling me invalid expression, it says invalid size. – Jack Faber Mar 22 '16 at 23:48
  • tried getline (cin, exp). It skipped everything and went back to the while loop. Also tried cin.getline(exp, sizeof(exp)). The compiler won't let me run that. It gives an error – Jack Faber Mar 23 '16 at 00:09
  • When I turn exp to a char[100], the cin.getline works, but the code exits with a assertion failure. – Jack Faber Mar 23 '16 at 00:31

1 Answers1

0

In your main loop, you're reading strings with the string extractor:

    cin >> exp;

THe string extractor is space sensitive. So as soon as a space char is encountered in the input, the string reading stops, and the witespace is not included in exp.

If you want to get a full line including spaces, you should opt for:

    getline (cin, exp);  

Edit:

The issue you experience with getline() is realted to the char extraction when you ask if user wants to use the calculator. Entering y is not sufficient. So you'l enter yenter. Only the y will be put into ans, so that getline() will start reading an empty line.

To solve this, update your initial input:

    cin >> ans;                   // as before 
    cin.ignore (INT_MAX, '\n');   // add this to skip everything until newline included

Here an online demo showing that it works (including error message in case of wrong operator)

Christophe
  • 68,716
  • 7
  • 72
  • 138
  • When I try this approach, the compiler completely skips allowing the user to enter the expression, and goes straight to the while loop check – Jack Faber Mar 23 '16 at 00:26
  • Could you please provide the expression that you're parsing ? – Christophe Mar 23 '16 at 00:27
  • using getline (cin, exp) I cannot type in an expression. The output says: please enter an expression: then it asks the user if they want to use the calculator – Jack Faber Mar 23 '16 at 00:30