Help with Postfix Calculator

This is a discussion on Help with Postfix Calculator within the C Programming forums, part of the General Programming Boards category; I am getting a segmentation fault 11 where my if and if-else statements begin in the EVALUATE function here. I've ...

  1. #1
    Registered User
    Join Date
    Jan 2012
    Posts
    14

    Question Help with Postfix Calculator

    I am getting a segmentation fault 11 where my if and if-else statements begin in the EVALUATE function here. I've only included the MAIN and the EVALUATE functions, as my other areas of the program I believe it be working properly. I am a novice to C and could use some help. Can anyone identify where I'm going wrong here?

    Code:
    // Evaluate
    void evaluate(char expression[]) {
        int counter = 0;
        while (expression[counter]) {
            char temp = expression[counter];
            if(expression[counter]=='+') add(); // if "+" then call addition. isn't advancing past this point.....
            else if(expression[counter]=='-') sub(); // if "-" then call subtraction.
            else if(expression[counter]=='*') mult(); // if "*" then call multiplication.
            else if(expression[counter]=='/') divide(); // if "/" then call division.
            else push(temp - '0'); // otherwise, assume it is number. push to stack.
            counter++;
        }
    }
    
    // Main
    int main() {
        printf("Enter the expression to be evaluated: ");
        char string[100];
        scanf("%s", string);
        evaluate(string);
        printFinal();
    }

  2. #2
    Registered User
    Join Date
    Mar 2011
    Posts
    546
    the code looks ok. better investigate your other functions to see what they are doing.

  3. #3
    Registered User
    Join Date
    Jan 2012
    Posts
    14
    Thanks. I solved my segmentation fault problem, and now my numbers are just skewed. I'm about to post the whole thing to see if anyone can help me figure out why. Thanks for the help, though, I may have kept looking in the same spots all day!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. postfix calculator
    By mrbains in forum C Programming
    Replies: 1
    Last Post: 01-26-2011, 06:57 AM
  2. what is a postfix calculator?
    By blogchama in forum C Programming
    Replies: 6
    Last Post: 05-16-2010, 03:23 AM
  3. Replies: 4
    Last Post: 03-12-2006, 02:17 PM
  4. Help with Postfix Calculator Project
    By hpy_gilmore8 in forum C Programming
    Replies: 5
    Last Post: 03-12-2003, 11:51 PM
  5. postfix calculator problem???
    By chanthee in forum C++ Programming
    Replies: 3
    Last Post: 02-26-2003, 04:08 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21