If conditional statement won't run.

Hi,

I'm making a program that converts inifx to postfix and then calculates the expression. But, for some reason my if statements dont run. It compiles, but the program only goes this far:

1
2
3
4
5
6
7
8

Please enter an expression to be calculated:
6+7
Your expression in Postfix is:
67+
Now calculating your expression ...
^C


its supposed to read out the total:

1
2
3
4
5
6
7

  cout << "Now calculating your expression ..." << endl;

  calculator (postfix, total);

  cout << "Your Expression " << expression << " or " << postfix << " = " << total << endl;


My original code:

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
  void calculator (string postfix, int & total) {

  stack<int> num_stack;

  for (int i =0; 1 < postfix.length(); i++) {
    if (postfix[i] != '+' || postfix[i] != '-' || postfix[i] != '*' || postfix[i] != '/') {
      num_stack.push(postfix[i]);
    }
    else if (postfix[i] == '+') {
      total = num_stack.top();
      num_stack.pop ();
      total = total + num_stack.top ();
      num_stack.push(total);
    }

    else if (postfix[i] == '-') {
      total = num_stack.top();
      num_stack.pop ();
      total = total - num_stack.top ();
      num_stack.push(total);
    }

    else if (postfix[i] == '*') {
      total = num_stack.top();
      num_stack.pop ();
      total = total * num_stack.top ();
      num_stack.push(total);
    }

    else /*(postfix[i] == '/')*/ {
      total = num_stack.top();
      num_stack.pop ();
      total = total /  num_stack.top ();
      num_stack.push(total);
    }
 }

  total = num_stack.top();

}


I did some trial and error and when i /* ... */ the for loop
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

  void calculator (string postfix, int & total) {

  stack<int> num_stack;
  int i = 0;

 // for (int i =0; 1 < postfix.length(); i++) {
    if (postfix[i] != '+' || postfix[i] != '-' || postfix[i] != '*' || postfix[i] != '/') {
      num_stack.push(postfix[i]);
    }
  /*  else if (postfix[i] == '+') {
      total = num_stack.top();
      num_stack.pop ();
      total = total + num_stack.top ();
      num_stack.push(total);
    }

    else if (postfix[i] == '-') {
      total = num_stack.top();
      num_stack.pop ();
      total = total - num_stack.top ();
      num_stack.push(total);
    }

    else if (postfix[i] == '*') {
      total = num_stack.top();
      num_stack.pop ();
      total = total * num_stack.top ();
      num_stack.push(total);
    }

    else /*(postfix[i] == '/')*/ {
      total = num_stack.top();
      num_stack.pop ();
      total = total /  num_stack.top ();
      num_stack.push(total);
    }
 } */

  total = num_stack.top();

}


and the first if executed and printed the couts, tried tweaking the else if parts but no luck.

Im probably missing something very simple (anticipating facepalm)

Any help will be appreciated!
Last edited on
Let b != c, then (a != b || a != c) is always true.

Case a != b:
(a != b || a != c) == (true || a != c) == true

Case a == b:
(a != b || a != c) == (false || a != c) == (a != c) == (b != c) == true

If you have a set of values {s1, s2, ..., sn} and you want to check that x is not equal to any of them, you need to check x != s1 && x != s2 && ... && x != sn.
yup facepalm moment. I put 1 < postfix.length() in the for loop instead of i .

thanks!
Topic archived. No new replies allowed.