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.
 ``1234567891011121314151617181920212223242526272829303132333435363738`` ``````void read_and_evaluate(istream& ins) { const char RIGHT_PARENTHESIS = ')'; const char LEFT_PARENTHESIS = '('; stack operators; stack 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<

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:
 ``123456789101112131415161718192021222324252627282930313233343536373839404142`` ``````void read_and_evaluate(istream& ins) { const char RIGHT_PARENTHESIS = ')'; const char LEFT_PARENTHESIS = '('; stack operators; stack 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<

I suggest that you read the description really carefully and translate it into code.
the order and the steps themself are there
 ``1234567891011`` ``````else if (strchr("+-*/", input.peek())) { input.get(mathOperators); do { cout< 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.
 ``1234567891011`` ``````else if (strchr("+-*/", input.peek())) { input.get(mathOperators); while((operators.empty() == false) && operators.top() != LEFT_PARENTHESIS && (orderOperations(mathOperators) > orderOperations(operators.top()))); { cout<

 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
 ``123`` ``````stack.empty() == false; stack.top() != '('; orderOperations(input.get() > orderOperations(stack.top()));``````

What I'm using for empty is this:
 ``123456789101112`` ``````template bool stack::empty() const { if(used > 0) { return false; } else { return true; } }``````

stack.top() looks like this:
 ``123456789101112`` ``````template T stack::top()const { if(empty()) { } else { return data[used-1]; } }``````

and my orderOperations looks like this:
 ``123456789101112131415`` ``````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.