C++ Infix to Postfix Error

I keep getting this error when I try running my program, it seems to be aborting at line 28 for some reason, any ideas?

error: Debug Assertion Failed!, Expression: deque iterator not dereferencable

ItoP.h
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
#ifndef ITOP_H
#define ITOP_H
#include <iostream>
#include <string>
#include <stack>
#include <fstream> 
using namespace std; 
class ItoP {
public:
	ItoP(); 
	string Convert(string);  
private:
	stack<char> mystack; 
	string output; 
	char temp;
};
ItoP::ItoP() {
}
string ItoP::Convert(string input) {
	cout << input <<endl;
		for(int i=0;i<=input.length();i++) { 
			if(input[i] == '0' || input[i] == '1' || input[i] == '2' || input[i] == '3' || input[i] == '4'
				|| input[i] == '5' || input[i] == '6' || input[i] == '7' || input[i] == '8' || input[i] == '9') {
				cout << input[i]; 
			cout << "25";
			}
			else
				cout << "28";
				if(input[i] == '(') {
					cout << "28";
				mystack.push(input[i]); 
				}
				else
					if(input[i] == ')') {
						while(mystack.top() != '(' && !mystack.empty())  {
							cout << "33";
							mystack.pop(); 
							cout << mystack.top();
						}
						cout << "37";
				mystack.pop();
					}
					else
						if(mystack.empty())  
						mystack.push(input[i]); 
						
						else 
							while(!mystack.empty() && input[i] <= mystack.top()) {
							mystack.pop();
							cout << mystack.top();  
						}
				mystack.push(input[i]);
				
}
		while(!mystack.empty()) {
			 mystack.pop();
			cout << mystack.top();
		}
	
cout << endl;
return output; 
}
#endif 


ItoP.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
#include "ItoP.h"

int main() {
ItoP New; 
string input;
fstream File; 
File.open("ItoP.txt");
while(!File.eof()) {
getline(File,input); 
New.Convert(input.c_str()); 
}
File.close();
}


ItoP.txt
1
2
3
4
5
6
2 + 3 * 5
2 + 3 * 5 ^ 6
2 + 3 - 5 + 6 - 4 + 2 - 1
2 + 3 * (5 - 6) - 4
2 * 3 ^ 5 * 6 - 4
2 + 3 * 6 ^ 2
Last edited on
In several places you do something similar in effect to:

1
2
3
if ( !mystack.empty() )
     mystack.pop() ;
cout >> mystack.top();


Who says mystack isn't empty after the pop? I know in several cases (line 50, 57 in itop.h) it's guaranteed to be called on an empty stack.

Also, you shouldn't have definitions of member functions which are not inline in a header file.

What doesn't make sense to me is that i was given the algorithm to use for this program, and it looks to me that I matched it but maybe i'm off more than i think. Here's the logic:

Infix to Postfix Write a C++ program that will accept infix expressions (like 5*(4+8)) and convert them to postfix. You are to use Dijkstra's algorithm for converting.

Algorithm:

Read in 1 line into a string
Print the string
while there are characters left to process in the string do
-----
| get a token (skip over blanks)
| if the token is a digit then output(token)
| else
| -----
| | if the token is '(' then push(token)
| | else
| | -----
| | | if the token is ')' then
| | | -----
| | | | while the top item is not '('
| | | | pop(temp) and output(temp);
| | | | pop(temp)
| | | -----
| | | else
| | | -----
| | | | if the stack is empty then push(token)
| | | | else
| | | | -----
| | | | | while the stack is not empty
| | | | | and the priority (token) <= priority (top item on the stack)
| | | | | pop and output(temp)
| | | | | push(token)
| | | | -----
| | | -----
| | ----
| -----
-----

while the stack is not empty do pop and output(temp)


The pop's match up but as you said are definitely popping empty stack.
I don't think "pop and output" implies an order.

1
2
3
4
        while(!mystack.empty()) {
            cout << mystack.top() ;
            mystack.pop();
        }
i changed to order of the pops and that seemed to help, the program runs longer but will still hit that same error almost at the end of code. I though the point of the declaration: !mystack.empty() was supposed to keep it from doing this?
!mystack.empty() is not a declaration.

And if your stack is empty when it's called, but you then remove an element, you no longer know if the stack is empty until you make the query again.

Topic archived. No new replies allowed.