infix to postfix problem

else if (stringType == 2)
{
while (topValue != "(");
{
postFix.push_back(myStack.top());
if (!myStack.empty())
{
myStack.pop();
}
}

if (!myStack.empty())
{
myStack.pop();
}
std::cout<<"leaving 2"<< std::endl;
}

I'm getting an error right before it enters the while loop. I have a feeling its because I cannot make a comparison in such manner. Any clues on how to compare an element in a string to another element?
Post a compilable *short* code which reproduces the problem. There is a ton of missing information which could be the source of your problem.
Edit : Also use [code][/code] tags.
Last edited on
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
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
int arithmetic_expression::inputInfix(std::string inputString)
{
    std::vector<std::string> expression = parse_string(inputString);
    std::vector<std::string>postFix;
    postFix.reserve(expression.size()+1);
    std::stack<std::string>myStack;

    //--------------------------------------------------------
    // Prints off initial Infix Expression */
    std::cout << "Infix Expression = " << std::endl;
    for(int j=0; j< expression.size(); j++)
    {
        std::cout<<expression[j] << " ";
    }
    std::cout << std::endl;
    //--------------------------------------------------------

    std::string topValue;
    int precExpression;
    int precTop;
    int stringType,checker;
    int j;

    for ( j=0;j<expression.size();j++)
    {
        stringType = type(expression[j]);

        if (stringType == 1)
        {
            myStack.push(expression[j]);

            std::cout << "Top Stack: " << myStack.top() <<std::endl;
            std::cout << "1. Progress = " << std::endl;
            for(int j=0; j< postFix.size(); j++)
            {
                std::cout<<postFix[j] << " ";
            }
            std::cout << std::endl;
        }
        else if (stringType == 2)
        {
            while (myStack.top() != "(");
            {
                postFix.push_back(myStack.top());
                if (!myStack.empty())
                {
                    myStack.pop();
                }
            }

            if (!myStack.empty())
            {
                myStack.pop();
            }

            std::cout << "Top Stack: " << myStack.top() <<std::endl;
            std::cout << "2. Progress = " << std::endl;
            for(int j=0; j< postFix.size(); j++)
            {
                std::cout<<postFix[j] << " ";
            }
            std::cout << std::endl;
        }
        else if (stringType == 3)
        {
            if (!myStack.empty())
            {
                precExpression = getPrecedence(expression[j]);
                precTop = getPrecedence(myStack.top());

                if (precTop<precExpression)
                {
                    myStack.push(expression[j]);
                }
                else
                {
                    postFix.push_back(myStack.top());
                    myStack.pop();
                    myStack.push(expression[j]);
                }
            }
            else
            {
                myStack.push(expression[j]);
            }

            std::cout << "Top Stack: " << myStack.top() <<std::endl;
            std::cout << "3. Progress = " << std::endl;
            for(int j=0; j< postFix.size(); j++)
            {
                std::cout<<postFix[j] << " ";
            }
            std::cout << std::endl;
        }
       /* else if (stringType == 4)
        {
            std::cout<<"entering 4"<< std::endl;

            postFix.push_back(expression[j]);

            std::cout<<"leaving 4"<< std::endl;
        }*/
        else if (stringType == 5)
        {
            postFix.push_back(expression[j]);

            if (!(myStack.empty()))
            {
                std::cout << "Top Stack: " << myStack.top() <<std::endl;
            }
            std::cout << "4. Progress = " << std::endl;
            for(int j=0; j< postFix.size(); j++)
            {
                std::cout<<postFix[j] << " ";
            }
            std::cout << std::endl;
        }

    }//end for

    std::cout<<"left for loop"<<std::endl;
    while (!myStack.empty())
    {
        postFix.push_back(myStack.top());
        myStack.pop();
    }

    //--------------------------------------------------------
    // Prints off initial Infix Expression */
    std::cout << "New PostFix Expression = " << std::endl;
    for(int j=0; j< postFix.size(); j++)
    {
        std::cout<<postFix[j] << " ";
    }
    std::cout << std::endl;
    //--------------------------------------------------------

    int valid = createExpressionTree(postFix);
    if (!valid) {
        topPtr = NULL;
    }

    return(valid);

}


Functions used for program

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
int type(std::string type)
{
    if (type == "(")
    {
        return 1;
    }
    else if (type == ")")
    {
        return 2;
    }
    else if (type == "+" || type == "-"|| type == "*" || type == "/") //Operators
    {
        return 3;
    }
    else if (type == " ")
    {
        return 4;
    }
    else //Operands
    {
        return 5;
    }
}

int getPrecedence(std::string type)
{
    if (type == "+" || type == "-")
    {
        return 1;
    }
    else if (type == "*" || type == "/")
    {
        return 2;
    }
    else if (type == "(")
    {
        return 0;
    }
}


Sorry it isn't exactly short
Topic archived. No new replies allowed.