Thread: I dont understand why this wouldn't work

  1. #16
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    I'd very much appreciate it if you could help. I really don't understand what's the problem. The infix2postfix and evaluate functions work fine I guess. So maybe it really has something to do with main.

  2. #17
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    @quzah: I think I terminated the string right. :|

  3. #18
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I would advise testing your functions one at a time, not just piling them all into the same program and hoping they work. If he's getting random odd characters in his output, then that typically you aren't terminating correctly. That's also why I included quotes around the string I was trying to display. That way if you see anything weird, like a quote down on a line by itself, you know you have a newline character in there.

    Also, you can add some printf lines temporarily to help you troubleshoot. For example:
    Code:
    	                default:
    	                    printf("\nInvalid Operator");
    	                    return 0;
    	            }
    Could be:
    Code:
    	                default:
    	                    printf("\nInvalid Operator: %d \'%c\'", *p, *p );
                                printf("\nevaluate( \"%s\" )\n", postfix );
    	                    return 0;
    	            }
    Oh, also you never actually clear out your stack properly. You just set top to -1. Who knows what values it actually contains. So you have to also be sure you aren't using your junk filled stack incorrectly.

    Quzah.
    Last edited by quzah; 03-01-2011 at 08:53 PM.
    Hope is the first step on the road to disappointment.

  4. #19
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Ok, I still can't fix the problem. Help. T___T

  5. #20
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Run this through a debugger, following line by line what's happening with every variable. If you don't know how to use a debugger, learn to ASAP. Debuggers are a godsend for programmers. In the mean time, put some print statements in your functions to display what push and pop are doing (or trying to do). Do likewise throughout infix2postfix and evaluate. I put some print statements in push and pop, and found your code to be popping from an empty stack. Your pop function returns a special value (EMPTY) for this, but you ignore it in a couple places. You should print a detailed error message when things like this happen.

  6. #21
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Honestly I ran this through a debugger as well and the problem could be something as innocuous as incrementing p too many times.

    Suggestion: follow quzah's advice about clearing the stack. Actually, fill it with something mundane when it's empty, so you can detect problems with it. The post variable is terminated through p, but p is messed up, so post is not terminated correctly. post has stuff like "1 2 3 + * 4 - \377\0007rfa8tu" in it after your infix2postfix function.*

    That's a place to start. Good luck.

    *Not an actual value of post from my debugger. The junk doesn't matter apart from being there.

  7. #22
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    If post is printed as "123*+34-" && we enter "123*+4-" as exp, it is safe to conclude that infix2postfix has no bug because it outputted a correct postfix expression. Now, when the program was used to evaluate exp (i.e. 123*+4-), it also outputted a correct answer (i.e. 3). So it is safe to conclude that evaluate also has no bug.

    I think the real problem is this: what characteristic is in "exp" array that's not in the "post" array? The only difference I see is that, "exp" was entered by the user, while "post" (despite being displayed as the same with "exp" like I said in my example earlier) was taken using the infix2postfix function.


    Maybe it has something to do with the gets() or the fflush()?

  8. #23
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Ok whiteflags, I'll try that. Thanks for the advice.

  9. #24
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Quote Originally Posted by We'reGeeks View Post
    If post is printed as "123*+34-" && we enter "123*+4-" as exp, it is safe to conclude that infix2postfix has no bug because it outputted a correct postfix expression. Now, when the program was used to evaluate exp (i.e. 123*+4-), it also outputted a correct answer (i.e. 3). So it is safe to conclude that evaluate also has no bug.

    I think the real problem is this: what characteristic is in "exp" array that's not in the "post" array? The only difference I see is that, "exp" was entered by the user, while "post" (despite being displayed as the same with "exp" like I said in my example earlier) was taken using the infix2postfix function.


    Maybe it has something to do with the gets() or the fflush()?
    Nope. While gets is a horrible function and should die, and fflush(stdin) is undefined (Salem made this clear in post #2 -- I sincerely hope you have corrected this by now), they're not your problem. Your problem is infix2postfix. As I pointed out, the string in post has an extra garbage character at the end. The fact that you're in Windows might mean you are using some character encoding that doesn't print this bogus character, so you don't see it, but it's there. Use the following print function on post to see what exactly is in it:
    Code:
    void print_hex(char *s)
    {
        while (s && *s) {
            printf("%02x ", *s++);
        }
    }
    You'll see something like ffffffff near the end, which is the -1 that pop is returning that you're putting in post somewhere in infix2postfix.

    .EDIT: You also have some trailing spaces in post that confuse evaluate(). Those trailing spaces don't exist in exp
    Last edited by anduril462; 03-02-2011 at 01:37 AM.

  10. #25
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    I see. (Oh dear, I'm an idiot, lol)

    Anyways, I don't know how to correctly terminate *p. Suggestions please....I thought that putting
    Code:
     *p = '\0';
    would do it.

  11. #26
    Registered User
    Join Date
    Nov 2010
    Location
    Long Beach, CA
    Posts
    5,909
    Yes, *p = '\0' terminates your string, but it only terminates it, so C can deal with it as a string. It doesn't magically remove garbage at the end. You just put a null character in there after the garbage.

    Your problem is somewhere in infix2postfix. Everywhere you use a pop in there, you need to make sure you're not putting the -1 (EMPTY) value returned by pop when the stack is empty into the post array (into *p).

    That's all I got for now. Sleepy time...

  12. #27
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Hey I tried this:

    Code:
                while(!isempty(&X))
    	    {
    	        n1 = pop(&X);
    	        *p = n1;
    	        if(!isempty(&X)) //added this
    			p++;
    
    	    }
    		
    	    *p = '\0';
    It works if there are more than 2 operands but if there are only 2, it doesn't work.
    Can anyone help?

  13. #28
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Why are you only incrementing if it's not empty? You should always increment, because your loop will stop when it's empty, which would properly null terminate it.
    Code:
    while( !isempty( &X ) )
    {
        *p = pop( &X );
        p++;
    }
    *p = '\0';
    Quzah.
    Hope is the first step on the road to disappointment.

  14. #29
    Registered User
    Join Date
    Nov 2010
    Posts
    28
    Ok, finally got it.

    Here's what I did:

    Code:
                 int j;
                 for(j=0; !isempty(&X); j++)
    	    {
    			if(j>0)
    			{
    				n1 = pop(&X);
    				*p = n1;
    				if(!isempty(&X))
    					p++;
    			}
    			else
    			{
    				n1 = pop(&X);
    				*p = n1;
    				p++;
    			}
    	    }
    		
    	    *p = '\0';
    	}
    It's working fine in my IDE.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. strcmp returning 1...
    By Axel in forum C Programming
    Replies: 12
    Last Post: 09-08-2006, 07:48 PM
  2. getline() don't want to work anymore...
    By mikahell in forum C++ Programming
    Replies: 7
    Last Post: 07-31-2006, 10:50 AM
  3. Why don't the tutorials on this site work on my computer?
    By jsrig88 in forum C++ Programming
    Replies: 3
    Last Post: 05-15-2006, 10:39 PM
  4. fopen();
    By GanglyLamb in forum C Programming
    Replies: 8
    Last Post: 11-03-2002, 12:39 PM
  5. DLL __cdecl doesnt seem to work?
    By Xei in forum C++ Programming
    Replies: 6
    Last Post: 08-21-2002, 04:36 PM