Closed Thread
Results 1 to 4 of 4

Thread: need someone to check my coding

  1. #1
    debug is offline Newbie
    Join Date
    Aug 2007
    Posts
    17
    Rep Power
    0

    need someone to check my coding

    im writing a stack to check for parenthesis but the output alway read " expression not match" even if the stack is balanced. hope someone can see what is wrong with my program.below is the coding
    Code:
    #include <iostream>
    #define STACKSIZE 80
    #define TRUE 1
    #define FALSE 0
    
    using namespace std;
    
    void StackInit();
    int IsStackEmpty();
    int IsStackFull();
    int StackPush(char c);
    char StackPop();
    char ViewStackTop();
    
    char myStack[STACKSIZE];
    int top;
    
    int main()
    {
    	char *c;
    	c = myStack;
    	int balance;
    
    	cout << " Please enter an equation:";
    	cin.getline(myStack, STACKSIZE);
    	for (int i = 0; i < STACKSIZE; i++)
    	{
    		if(*c == '(' || *c == '{' || *c == '[')
    		{
    			StackPush(*c);
    		}
    		else
    		{  
    			if(*c == ')' || *c == '}' || *c == ']')
    			{
    				if(IsStackEmpty() == TRUE)
    				{
    					balance = FALSE;
    				}
    				else
    				{
    					StackPop();
    				}
    			}
    		}
    	}
    
    	if(balance = FALSE)
    	{
    		cout << "Result: Expression not match";
    	}
    	else
    	{
    		if(!IsStackEmpty())
    		{
    			cout << "Result: Expression not match";
    		}
    		else
    		{
    			cout << "Result Expression is vaild";
    		}
    	}
    
         system("pause");
         return 0;
    }
    
    void StackInit()
    {
      top = -1;
    }
    
    int IsStackEmpty()
    {
      if (top == -1)
        return TRUE;
      return FALSE;
    }
    
    int IsStackFull()
    {
      if (top == (STACKSIZE - 1))
        return TRUE;
      return FALSE;
    }
    
    int StackPush(char c)
    {
      if (top == (STACKSIZE - 1))
        return FALSE;
      myStack[++top] = c;
      return TRUE;
    }
    
    char StackPop()
    {
      if (top == -1)
        return '\0';
      return myStack[top--];
    }
    
    char ViewStackTop()
    {
      if (top == -1)
        return '\0';
      return myStack[top];
    }
    Last edited by v0id; 02-29-2008 at 04:57 AM.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Posts
    Many

     
  3. #2
    v0id is offline Retired
    Join Date
    Apr 2007
    Posts
    2,937
    Blog Entries
    3
    Rep Power
    42
    Please use code-tags in the future.

    I didn't go through the whole code, but I noticed how you're trying to compare balance with FALSE, in one of the if-statements.
    Code:
    	if(balance = FALSE)
    	{
    		cout << "Result: Expression not match";
    	}
    You're not comparing, but assigning. This should of course be:
    Code:
    	if(balance == FALSE)
    	{
    		cout << "Result: Expression not match";
    	}

  4. #3
    debug is offline Newbie
    Join Date
    Aug 2007
    Posts
    17
    Rep Power
    0
    thks for the reply but if i use balance == false, i will get some kind of run time error.

  5. #4
    Join Date
    Jul 2006
    Posts
    16,478
    Blog Entries
    75
    Rep Power
    143
    Getting a runtime error just tells you that you have another error in your code. Not getting runtime errors doesn't mean it's correct.
    Programming is a branch of mathematics.
    My CodeCall Blog | My Personal Blog

Closed Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Please check my coding error
    By farhanyun91 in forum Java Help
    Replies: 41
    Last Post: 08-13-2010, 12:41 PM
  2. How to check if a check box has changed?
    By Peckerfish in forum C# Programming
    Replies: 2
    Last Post: 05-24-2009, 02:22 PM
  3. WGA check on IE and WMP
    By aftertaste in forum Linux/Unix General
    Replies: 1
    Last Post: 04-05-2009, 04:16 PM
  4. Check your IP
    By Jaan in forum PHP Tutorials
    Replies: 7
    Last Post: 01-17-2008, 06:29 AM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts