Infix to Postfix using file input & stack

I'm using a test file before I use the one my teacher gave me.
My test file looks like this
3
3+1
(3+1)
Now I'm trying to take the file, read it line by line and turn the infix equations into postfix equations. In other words, they're suppose to end up looking like this:
3
31+
31+
The assignment is to utilize a non-STL stack to make it so.
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
void read_and_evaluate(istream& ins)
{
	const char RIGHT_PARENTHESIS = ')';
	const char LEFT_PARENTHESIS = '(';

	stack<char> operators;
	stack<int> operands;
	int operand = 0;
	char symbol;

	// Read through the file, line by line
	do
	{
		ins.get();
		do
		{
			if(ins.peek() == LEFT_PARENTHESIS)
			{
				//read left paren and push onto stack
				ins>>symbol;
				operators.push(symbol);
			}
			//next input is a number/other operand
			else if(isdigit(ins.peek()))
			{
				//read operand and write it to output
				ins>>operand;
				cout<<operand<<endl;
			}
			else if (strchr("+-*/", ins.peek()))
			{
				cout<<operators.top();
				operators.pop();
			}
			// Read next input and push it onto the stack
			operators.push(ins.peek());
		}while(ins.peek() == NULL);
	}while(!ins.eof());


Here's the pseudocode I'm using from my textbook so you can see what I'm referencing:

1. Initialize a stack of characters to hold the operation symbols and parentheses
2. do
if(the next input is a left parenthesis)
Read the left parenthesis and push it onto the stack
else if( the next input is a number or other operand)
Read the operand and write it to the output
else if( the next input is one of the operation symbols)
{
do
Print the top operation and pop it
while none of these three conditions are true:
1. The stack becomes empty, or
2. The next symbol on the stack is a a left parenthesis, or
3. The next symbol on the stack is an operation with lower precedence
than the next input symbol
Read the next input symbol, and push this symbol onto the stack
}
else
{
Read and discard the next input symbol(which should be a right
parenthesis).
Print the top operation and pop it; Keep printing and popping until
the next symbol on the stack is a left parenthesis.(If no parenthesis
is encountered, then print an error message indicating unbalanced
parentheses, and halt.) Finally, pop the left parenthesis.
}
while(there is more of the expression to read);
3. Print and pop any remaining operations on the stack. (There should be no remaining left parentheses; if there are, the input expression did not have balanced parentheses.)


So far I'm only reading and outputting numbers but it doesn't output the operators. My output looks like this right now:

3
3
1
31

I'm trying to figure out why it doesn't want to output the operators at all. This is only my third semester programming, so I'm still pretty new at this. Is there something that I'm doing wrong or something that I haven't thought of yet?
You're doing some strange things there.

get() removes the character from the stream while peek() doesn't. See

http://www.cplusplus.com/reference/istream/istream/?kw=istream

if you have an NULL character you will have an infinite loop.

In case of an operator. Well, you print the last symbol on the stack, not the operator. so if the operator is last it won't be printed at all
I guess now I'm at a complete loss on this project then. I don't it would be so bad if the file we're suppose to read from didn't start with an empty line. This was the only way I could see it working unless there's something out there that I haven't found yet.
Thig might give you a direction:
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
void read_and_evaluate(istream& ins)
{
	const char RIGHT_PARENTHESIS = ')';
	const char LEFT_PARENTHESIS = '(';

	stack<char> operators;
	stack<int> operands;
	int operand = 0;
	char symbol;
	char op_symbol = 0;

	// Read through the file, line by line
  while(ins.good())
  {
    ins.get(symbol);
    if((symbol == LEFT_PARENTHESIS) || (symbol == RIGHT_PARENTHESIS))
    {
      // Ignored
      // while your requirements demand it
    }
    else if(isdigit(symbol))
    {
      operators.push(symbol);
      cout<<symbol;
      if(op_symbol != 0)
      {
        cout<<op_symbol;
        operators.push(op_symbol);
        op_symbol = 0;
      }
    }
    else if (strchr("+-*/", symbol))
    {
      op_symbol = symbol;
    }
    else
    {
      cout<<"Invalid symbol"; // Just a hint might be removed
      break;
    }
  }
}


I suggest that you read the description really carefully and translate it into code.
the order and the steps themself are there
1
2
3
4
5
6
7
8
9
10
11
else if (strchr("+-*/", input.peek()))
{
	input.get(mathOperators);
	do
	{
		cout<<operators.top();
		operators.pop();
	}while(!operators.empty() && operators.top() != LEFT_PARENTHESIS && (orderOperations(mathOperators) > orderOperations(operators.top())));	

// Read next input and push it onto the stack
operators.push(mathOperators);


Ok so here's my problem and I've been beating my head against the wall for hours on this. When I come upon the line 5+7, my program will read the 5 straight out to the screen on the first go around. However when it comes to the + it does what it's suppose to at first but instead of ignoring the do/while loop it just goes into it. The stack is empty so it should ignore this on the first go around but it doesn't. I can't seem to figure out where I'm going wrong on this one. Can anyone help me please?
but instead of ignoring the do/while loop it just goes into it.
that is what a do { ... } while(...); loop is supposed to do: it checks the condition after the content is processed.
A while(...) { ... } loop checks before the processing is done
"Face Palm" I need to become more familiar with while and do/while loops.
Ok so I've switched it to a while loop but it still processes it rather than skipping it. Maybe I've interpreted the conditions wrong.
1
2
3
4
5
6
7
8
9
10
11
else if (strchr("+-*/", input.peek()))
		{
			input.get(mathOperators);
		
			while((operators.empty() == false) && operators.top() != LEFT_PARENTHESIS && (orderOperations(mathOperators) > orderOperations(operators.top())));	
			{
				cout<<operators.top();
				operators.pop();
			}
		// Read next input and push it onto the stack
		operators.push(mathOperators);	



do
Print the top operation and pop it
while none of these three conditions are true:
1. The stack becomes empty, or
2. The next symbol on the stack is a a left parenthesis, or
3. The next symbol on the stack is an operation with lower precedence
than the next input symbol
Read the next input symbol, and push this symbol onto the stack

I interpreted this as meaning
1
2
3
stack.empty() == false;
stack.top() != '(';
orderOperations(input.get() > orderOperations(stack.top()));

What I'm using for empty is this:
1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
bool stack<T>::empty() const
{
	if(used > 0)
	{
		return false;
	}
	else
	{
		return true;
	}
}

stack.top() looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
template<class T>
T stack<T>::top()const
{
	if(empty())
	{
	
	}
	else
	{
		return data[used-1];
	}
}

and my orderOperations looks like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int orderOperations(char n)
{
	if(n =='*' || n =='/')
	{
		return 2;
	}
	if(n =='+' || n =='-')
	{
		return 1;
	}
	else
	{
		return 0;
	}
}

From what I'm seeing these should work or did I type something wrong? If so would someone kindly point out where? Thanks for the advice.
Topic archived. No new replies allowed.