RPN Expression Evaluator ...segmentation Fault

The code in its current form works just fine. But whenever I enable the commented out portion for the unary operators there is a segmentation fault at runtime.
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
71
72
73
74
75

#include<string>
#include<sstream>
#include<iostream>
#include<deque>
#include<cmath>
using namespace std;

int main()
{
    string input;
    getline(cin,input);
    deque<double> stack;
    istringstream in;
    in.str(input);
    double opn;
    char opt;
    string opt_s;
    for(;;)
    {
        if(in>>opn)
        {
            stack.push_back(opn);

            continue;
        }
        else
        {
            in.clear();
            if (in>>opt)
            {
                opn = stack.back();
                stack.pop_back();

            switch (opt)
            {
                case 'p':{opn += stack.back();  break;}
                case 'm':{opn=stack.back()-opn; break;}
                case '*':{opn *= stack.back();  break;}
                case '/':{opn=stack.back()/opn; break;}
                default:
                {}
            }
            stack.pop_back();
            stack.push_back(opn);
            }
//.........................
//                in.clear();
//                opn=stack.back();
//                stack.pop_back();
//                if(in>>opt_s)
//                {
//                    if(opt_s=="sin")stack.push_back(sin(opn));
//                }

///This BLOCK is NOT working as it should...Wonder Why ?  :(
//.....................................
            


            else break;
        }

    }
    opn = stack.back();
    stack.pop_back();

    if(stack.empty())
    cout<<opn;
    else
    cout<<"More Operands than Operators";
    cin.get();
    return 0;

}

Any idea about improvement is welcome.
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
if (in>>opt)
            {
                opn = stack.back(); 
                stack.pop_back(); //1

            switch (opt)
            {
                case 'p':{opn += stack.back();  break;}
                case 'm':{opn=stack.back()-opn; break;}
                case '*':{opn *= stack.back();  break;}
                case '/':{opn=stack.back()/opn; break;}
                default:
                {}
            }
            stack.pop_back(); //2
            stack.push_back(opn);
            }
//......................... Unary operator
//                in.clear();
//                opn=stack.back();
//                stack.pop_back(); //3
//                if(in>>opt_s)
//                {
//                    if(opt_s=="sin")stack.push_back(sin(opn));
//                }

///This BLOCK is NOT working as it should...Wonder Why ?  :(
//..................................... 
Every operator has a number of arguments. That is the numbers of pop_back that you should perform.
But in the case of an unary operator you are trying to perform 3 pop_backs.
Topic archived. No new replies allowed.