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.
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.
You're not comparing, but assigning. This should of course be:Code:if(balance = FALSE) { cout << "Result: Expression not match"; }
Code:if(balance == FALSE) { cout << "Result: Expression not match"; }
thks for the reply but if i use balance == false, i will get some kind of run time error.
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks