I don't think this should be seg faulting....what did I miss....?

Ok so what I'm trying to do, is take an infix notation string ex.(3+5/6) an turn it into postfix ex(3 5 6 / +). The thing is I have to account for incorrect characters which is anything other than an int, or one of the operators (/ * - + %). I go through with the if/if else statements checking for each, and there is an else statement at the bottom of that, line 59, that should return invalid if the character isn't in the set of allowed. It will run(incorrectly) if I don't uncomment line 62, but thats what I need to work. It gives me a seg fault, and I don't have a clue why, I don't think it should be....it runs the print statements, and shows that the character is incorrect, but seg faults at line 62. Any ideas...? The equation it is stuck on is (2 $ 2) - should return "invalid"

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
  string ExpressionManager::infixToPostfix(string infixExpression){
	cout<< " 1.0 ";
	string Output;
	string Current;
	stack<string> st;
	stringstream ss;
	ss << infixExpression;
	string o1 = "*/%";
	string o2 = "+-";
	string close = "}])";
	string open = "{[(";

	cout<<" "<<infixExpression<<" ";
	while(ss >> Current){
		cout<<" 2.0 ";
		if(isInteger(Current)){
			cout<<" 2.1 ";
			Output = Output + " " + Current;
		}
		else if(open.find(Current)!= std::string::npos){//if Current is open parenth.
			cout<<" 2.2 ";
			st.push(Current);
		}
		else if(close.find(Current)!= std::string::npos){//if Current is close parenth.
			cout<<" 2.3 CURRENT:"<<Current;
			if(st.empty() == false){
				cout<<" 2.3.1 ";
				string temp = st.top();
				cout<<" TOP:"<<st.top()<<" ";
				while(open.find(st.top()) == std::string::npos && st.empty() == false){//while the st.top isn't an open parenth.
					cout<<" 2.3.1.1";
					cout<<st.top();
					cout<<" OUTPUT1:"<<Output;
					Output = Output + " " + st.top();
					cout<<" OUTPUT2:"<<Output;
					st.pop();

				}//end while
				st.pop();//this might be a bug later on
			}//end if
			else{
				cout<<" 2.3.2 ";
				return "invalid";
			}//end else
		}//end else if
		else if(o1.find(Current)!= std::string::npos){//if Current is an o1 operand
			cout<<" 3.0 ";
			while(open.find(st.top())!=std::string::npos || st.empty() == false){
				cout<<" 3.1";
				Output = Output + " " + st.top();
				st.pop();
			}//end while
			st.push(Current);
		}//end else if
		else if(o2.find(Current) != std::string::npos){//if Current is an o2 operand
			cout<<" 3.2 ";
			st.push(Current);
		}
		else{//if Current is an invalid character
			cout<<" 4.0 ";
			cout<<" INVALID CURRENT:"<<Current<<" ";
			//return "invalid";
		}
	}//end while
	while(st.empty() == false){

		cout<<" 5.0";
		cout<<" TOP IS THIS:"<<st.top()<<" ";
		//return "invalid";
		if(open.find(Current)!=std::string::npos){

			cout<<" 5.1 ";
			return "invalid";
		}
		else{
			cout<<" 5.2 ";
		Output =Output + " "+ st.top();
		st.pop();
		}
	}
	cout<<" AT END ";
	return Output;
}//end function 
Generally a segfault means that you corrupted memory and you were just lucky enough to have done it in a way that causes the segfault instead of your program continuing in a corrupted state.

Since you're getting the segfault on return statements, I would guess that you're corrupting the memory of one of the variables you create in your function, so that when the destructors try to release the memory you get the segfault. I'm not sure what exactly it is, though, I'd need to look over the code more thoroughly.
Topic archived. No new replies allowed.