title: Infix to postfix evaluating unary negative
body: <p>I'm sure this has been asked a few times but the other questions I looked at didn't really help me much. Alright so here goes: I've got three functions one that converts an infix expression to a postfix, one that's a preprocessor and one that evaluates the postfix expression. What I'm having trouble with is evaluating a unary negative expression. If I put in my entire code it'll be super long so I'm only going to post up the parts that deal with the negative/minus case:</p>
  
  <p>here's my output:
       input: -3<br>
       after preprocess:  3 
       postfix = -3
       then a segmentation fault when it should output  " total = -3 "</p>
  
  <pre><code>#include "postfix.h"
  #include "stack.h"
  #include &lt;cstdlib&gt;
  #include &lt;cmath&gt;
  #include &lt;cstdio&gt;
  
  void eval_postfix(char* postfix){
    Stack&lt;double&gt; stack;
    char fchar[100];
    int j=0;
    double a, b, convert, total = 0.0;
    for(int i=0; postfix[i] != '\0'; i++){
      switch(postfix[i]){
      case '-':
        a = stack.top();
        stack.pop();
        b = stack.top();
        stack.pop();
        total = b-a;
        stack.push(total);
        break;
  </code></pre>
  
  <p>I'm pretty sure the error is in that part of the function, I've been trying different things but nothing has been working, more times than not I get a segmentation fault or a zero. I originally tried to apply what I did in the infix2postfix expression (which obviously didn't work) But here's the rest of my code for the negative/minus case...</p>
  
  <pre><code>void infix2postfix(char* infix, char* postfix){
    Stack&lt;char&gt; stack;
    stack.push('\0');
    int pc = 0;
    bool c;
    for(unsigned int i = 0; infix[i] != '\0'; i++){
      //use the switch method to define what to do for each of the operators
      switch(infix[i]){
   case '-':
        c = 0;
        //unary negative
        if(i==0){
      postfix[pc++] = infix[i];
      c = 1;
        }
        else if((infix[i-1] == '*' ||
             infix[i-1] == '^' ||
             infix[i-1] == '*'  ||
             infix[i-1] == '/'  ||
             infix[i-1] == '+' ||
             infix[i-1] == '-')&amp;&amp; 
            i&gt;0){
      postfix[pc++]= infix[i];
      c=1;
        }
        else{
      if(stack.top() == '*' || stack.top() == '/' || stack.top() == '^'){
        while(stack.top() != '\0' &amp;&amp; stack.top() != '('){
          postfix[pc++] = stack.top();
          postfix[pc++] = ' ';
          stack.pop();
        }
      }
        }
        if (c==0)
      stack.push('-');
        break;
  
  void preprocessor(char* input){
        char output[100];
        int oc = 0;
        for(unsigned int i=0; input[i] != '\0'; i++){
          if((input[i] == '-' &amp;&amp; (input[i-1] == '*' || input[i-1] == '^' || input[i-1] == '*'
                      || input[i-1] == '/' || input[i-1] == '+' || input[i-1] == '-')
          &amp;&amp; i&gt;0)){
            //output[oc++] = '0';
            output[oc++] = input[i];
          }
  </code></pre>
  
  <p>I'm almost certain that whatever error it is I made (or whatever edit I need to do) is probably something really simple that I just can't see (cause that's usually the case with me) but any nudge in the right direction would be highly appreciated!</p>
  
  <p>**Note: the formatting of my code may not be accurate cause I only copied and pasted the parts I felt were relevant.</p>