Infix to Postfix Segmentation Fault! Help Appreciated!

The code compiles and works for the most part . . . but when the user inputs "3 + 4" as opposed to "3+4" the program results in a segmentation fault. Where did I go wrong?

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

#include <iostream>
#include "stack.h"
#include "operatorPriority.h"
using namespace std;

int main()
{
    Stack operatorStack;    //Stack to hold operators
    string postfix;         //String to hold conversion
    string infix;           //String to hold input equation
    char curr;             //Current char being examined
    char temp;             //Char to hold operator for comparison
    int count = 0;         //Char to be evalutated in string
    int stringLength = 0;  //Length of infix string
    cout << "Infix to postfix converter. Type 'q' to quit." << endl;

    while (1)
    {
        cout << "   >>";
        getline(cin, infix);

        stringLength = infix.length();

        if (infix[0] == 'q')
        {
            return 0;
        }

        operatorStack.Push('(');
        infix += ')';

        count = 0;
        while(count <= stringLength) //!operatorStack.IsEmpty())
        {
            curr = infix[count];
            if(curr =='(')
            {
                operatorStack.Push(curr);
            }
            else if(IsANum(curr))
            {
                postfix += curr;
            }
            else if(curr == ')')
            {
                temp = operatorStack.Pop();
                while(temp != '(')
                {
                    postfix += temp;
                    temp = operatorStack.Pop();
                }
            }
            else //Must be an operator!
            {
                if(!IsANum(curr) && !IsOperator(curr) && curr != ' ')
                {
                    cout << "   ERROR: Invalid character!" << endl;
                    return 0;
                }
                while(Priority(operatorStack.Top()) >= Priority(curr))
                {
                    temp = operatorStack.Pop();
                    postfix += temp;
                }
                operatorStack.Push(curr);
            }
            count ++;
        }

        if(!operatorStack.IsEmpty())
        {
            cout << "   ERROR: Mismatched parens!" << endl;
        }

        cout << "   " << postfix << endl << endl;

        postfix.clear();
    }

    return 0;
}
Topic archived. No new replies allowed.