I am seeking some help on validation opening/closing brackets.
I had the entire validation working until my professor said that my Pop function had to be a "void" not a "Char". Which destroyed my previous validation and now I have no idea how to validate this.
I will only be posting the validating function.
All the characters are being thrown in a Stack. So I am using Pop/Top/Push/isEmpty/isFull. That will be shown in my validating code.
/* Will be reposting the code back up when assignment is past its due date or I finish the course*/
}
I am pretty positive I must have over complicated my validation. I am not the best coder alive, so if there is any ways to type this up in a cleaner fashion or easier method. Please throw that out there.
If you can't assign the result of Pop() to anything, I'd guess it must just remove the value without returning it. You probably need to use a Top() method or something similar to examine the top of the stack and see if it the matching brace type. (e.g., strExp[i] == '}' && stackA.Top() == '{'
my professor said that my Pop function had to be a "void" not a "Char"
The obvious solution is to pass back the popped value as an output parameter, rather than as a return value. That seems like a pointless bit of obfuscation to me, but I guess it's a learning exercise rather than production code.
Thanks for the replies guys. Sadly enough, I am still at the same exact spot.
@Zhuge I had already tried that. I created a char variable and equaled it to stackA.Top() and that time I did not get an error but the result is the same "[ )" is considered valid. I am now starting to think that my push/Top/Pop functions are the ones that are wrong.
@MikeyBoy Yea, he tends to dance around the main point and never gives you a straight answer to it. It is how one learns but sometimes a straight answer is all that one needs haha.
I just can't seem to be able to find a way to write a different variable for strExp[i] == '[/{/(' Left Brackets/Para. Before, when I made Pop a char, it was simple and everything worked nicely but this whole Pop being a void is killing me :/ . Been trying to figure this out for hours, and I know its something so basic.
Post how you tried to use Top(); it may be you used it wrong. If you think your stack code may be the problem, though, you may want to post that as well.
I'd check the condition on lines 23-25. When does that evaluate to true? When does it evaluate to false?
Let's say that leftbracket is '{' and strExp[i] is '}'. The part of the expression before the first logical or is true, so the entire expression inside the parentheses evaluates to true. Good so far. Then you negate the value inside the parentheses so that the expression becomes false -- not so good.
@Cire Thanks for the advice, made me think so hard going step by step on the stack and "if" statements. I had to reread your comment like 10 times because am to dumb to understand it at first. To be honest, I still don't think I fully understand it haha but it did help me with my issue. Thank you
This is what I changed/added
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19
/* will be deleting this soon */
good = true; //added this
for (int i = 0; i < S_SIZE; i++)
{
//rest of code
if (strExp[i] == '}' || strExp[i] == ']' || strExp[i] == ')')
leftbracket = stackA.Top();
stackA.Pop();
if (!(leftbracket == '{' && strExp[i] == '}' || leftbracket == '[' && strExp[i] == ']' || leftbracket == '(' && strExp[i] == ')'))
good = false; // Was good = true; but I changed it because of the If statement. If NOT left == '{' and strExp =='}' then false.
}
else
good = true; //I don't even know if this is even necessary since I stated good = true; outside the loop.
}
}
return good;
1 2 3 4
char CStack::Top() const
{
return data[top]; // Before it was return data[top+1] messing up the stack.
}
@Zhuge Thanks for the help, and as I mentioned to Cire, I had my Top function return data[top + 1] which messed up the stack. All I had to do was change it to data[top] and change a good = true; to false. -__- its always something so basic.
For any mod looking at this I will be deleting the code posted until the assignment is past its due date or after I finish the course. I will be posting the code up again. For reference to anyone having an issue or identical program validating brackets.