sudden exit

This program I'm working on just exits unexpectedly right after outputting "Expression?". Any thoughts on what might be the problem?

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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
int main(void)
{
    provideHelpIfNecessary();
    while(true){
    cout << "Expression? ";
    string expr;                    // holds the supposed expression
    getline(cin, expr);
    if (expr.length() == 0)
        break;
    try {
        ValueType result = processExpression(expr);
        cout << result << endl;
    } catch (ZeroDivideException ex) {
        cerr << "Attempt to divide by zero!\n";
    } catch (IllegalExprException ex) {
        cerr << "Illegal infix expression!\n";
    }
    }  // end while
    cout << endl;
} // end main

// void provideHelpIfNecessary(void)

ValueType processExpression(const string& expr)
   throw (IllegalExprException, ZeroDivideException)
{
    stack<ValueType> ValStack;
    stack<char> OpStack;
    for (int i = 0; i < expr.length(); i++)
        {
            if (expr[i] == '0'|| expr[i] == '1'|| expr[i] == '2'||
                expr[i] == '3'|| expr[i] == '4'|| expr[i] == '5'||
                expr[i] == '6'|| expr[i] == '7'|| expr[i] == '8'||
                expr[i] == '9') ValStack.push(expr[i]);
            if (expr[i] == '(') OpStack.push(expr[i]);

            if (expr[i] == '*'|| expr[i] == '+'||
                expr[i] == '-'|| expr[i] == '/') {

                if(OpStack.empty()) OpStack.push(expr[i]);

                else if (precedence(expr[i]) > precedence(OpStack.top()))
                    OpStack.push(expr[i]);

                else
                    {
                        if (!OpStack.empty() && precedence(expr[i]) <= preceden\
ce(OpStack.top())) execute(ValStack,OpStack);
                        OpStack.push(expr[i]);
                    }}
            if (expr[i] == ')') {
                if (OpStack.top() != '(') execute(ValStack,OpStack);
                OpStack.pop();
            }}

    if (!OpStack.empty()) execute(ValStack,OpStack);
    ValueType result = ValStack.top();
    return 0;
} // end processExpression

// bool isValidResponse(char response)
// bool isYesResponse(char response)

int precedence(char op)
{
    if (op == '*' || op == '/') op = 3;
    if (op == '+' || op == '-') op = 2;
    if (op == '(') op = 0;
    else op = -1;
    return op;
} // end precedence

void execute(stack<ValueType>& valStack, stack<char>& opStack)
   throw (IllegalExprException, ZeroDivideException)
{
    ValueType result;
    ValueType operand2 = valStack.top();
    valStack.pop();
    ValueType operand1 = valStack.top();
    valStack.pop();
    char op = opStack.top();
    opStack.pop();
    result = doOperation(operand2,op,operand1);
    valStack.push(result);

} // end execute

ValueType doOperation(ValueType operandL, char operation, ValueType operandR)
   throw (IllegalExprException, ZeroDivideException)
{
    ValueType ans;
    if (operation == '*') ans = operandL * operandR;
    if (operation == '/') ans = operandL / operandR;
    if (operation == '+') ans = operandL + operandR;
    if (operation == '-') ans = operandL - operandR;
    return ans;
} // end doOperation 


And I'm not allowed to change anything in main().
closed account (D80DSL3A)
Have you verified that expr.length() != 0 ? Otherwise lines 8 and 9 would cause the sudden exit.
Perhaps provideHelpIfNecessary(); left a '\n' in the input stream?
Okay, so I tried two things:

1) I tried changing getline(cin, expr); to cin >> expr; just to see if that would change anything. And the program worked, but of course it didn't work correctly since there might be spaces in an input.

2) I took out lines 8 & 9. And after "Expression?" it gave me a segmentation fault.
Topic archived. No new replies allowed.