You just have a few extra closing curly parentheses and an extra semicolon.
I think, if you try to thoroughly indent it, row by row, you should be able to spot them by yourself. I might be wrong, but I don’t see other (big) errors.
It’s not perfect, but it should work.
I made a few changes to the version to pointed me to. I am not good with proper indentation. I just want to make sure this is a queue though ? i already have a stack version done
#include <iostream>
#include <string>
#include <stack>
#include <queue>
usingnamespace std;
bool isBalanced(string s);
int main()
{
cout << "This program checks a string to see if its parentheses are properly \n";
cout << "balanced.";
string str;
cout << "\nType in a string with some parenthesis:\n";
getline(cin, str);
if (isBalanced(str))
cout << "The string has balanced parentheses.";
else
cout << "The string does not have balanced parentheses.";
return 0;
}
bool isBalanced(string str)
{
queue<char> charQueue;
for (unsignedint k = 0; k < str.length(); k++)
{
switch (str[k])
{
case'(':
// Put left paren on stack
charQueue.push(str[k]); break;
case')':
if (charQueue.empty())
returnfalse;
else
charQueue.pop();
break;
default:
;
}
}
if (charQueue.empty())
returntrue;
elsereturnfalse;
}
The algorithm is literally identical to the stack version. All you have to do is start with the stack version and change the word "stack" to "queue". Done.
Actually you don't need a stack or queue at all. But that's the assignment.
It doesn't matter what or how the program compiles. It matters what the source code says. After all, the assignment requires that you deliver source code, and not a compiled program.
Anyway, every programmer works under the assumption that the compiler properly follows the instructions in the source code. Your program tells the compiler to use a queue (on line 31). Therefore, the compiled program uses a queue.
Just because a program compiles doesn't mean it is correct. Test it. Run the program repeatedly, and try to type in an input that makes the program produce the wrong answer.
> It compiles. But want to make sure that it is compiling as a queue rather than stack. I can’t tell the difference
You're an easy mark then at a poker game if you can't tell the difference between the word "stack" and the word "queue".
The sky is pink, and you have neither the nouse nor skill to even tell whether I'm telling the truth or not.
Line 44 in your latest version isn't quite correct (where you call charQueue.pop()). Compare this to your working stack version and you should see what's wrong.