Calculator pt2

I need the loop to continue after "Wrong Expression!" and "Error. Insufficient operators for operands." allowing them to keep entering expressions until they are ready to quit

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#include <stack>
#include <iostream>
#include "calc_useful.h"
using namespace std;


int main()
{
  while(true)
  {
    // declare your stack here
    stack<int> stk;
    char c;
    int onenum, twonum;
    cout<<"Please enter your expression:\n";

    c = cin.get();
    while(c != '\n')
    {
      if(isdigit(c))
      {
        cin.putback(c);
        cin>>onenum;
        stk.push(onenum);
        // stack operation here.
      }
      else if(isop(c))
      {
        // if the stack is empty you will get a error.
      if(stk.empty())
      {
        cout<<"Wrong Expression!"<<endl;
	return 0;
      }
      twonum = stk.top();
      stk.pop();
      if(stk.empty())
      {
        cout<<"Wrong Expression!"<<endl;
	return 0;
      }
      onenum = 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;
  }
}

cu.c
bool isop(char op){
        return op =='+' || op == '-' || op == '*' || op == '/';
}

int evaluate(int num1, int num2, char op){
    if(op == '+') return num1 + num2;
    if(op == '-') return num1-num2;
    if(op == '*') return num1*num2;
    if(op == '/') return num1/num2;
    else return 0;
}

double evaluate(double num1, double num2, char op){
    if(op == '+') return num1 + num2;
    if(op == '-') return num1-num2;
    if(op == '*') return num1*num2;
    if(op == '/') return num1/num2;
    else return 0;
}

cu.h---------------------------

double evaluate(double num1, double num2, char op);
int evaluate(int num1, int num2, char op);
bool isop(char op);
return 0 in main exits the program.

I think what you want is something like this:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
bool messedup = false;
while(true)
{
    messedup=false; //its a new loop, fresh start, all is good.
   ... 
    cout <"Wrong Expression" 
    messedup = true;
   ...
  if(!messedup)
     {
       process / do things
    } //else do nothing
  
}


or in short, replace return zero with the setting of a flag and
use that flag to prevent doing anything wrong due to attempted processing of bad input,
then reset and go again from the top.
Last edited on
so all of my code should be in the if statement?
That is not what he said, When you return 0, the program ends.
If that is the desired result then great, if it's not then you have to find another way.
Right now after wrong expression, your program stops.
I know all that, the point of the post is to help me find the other way and the other way he has suggested includes an if statement so I am asking him a question.
Topic archived. No new replies allowed.