Thread: question: program on balancing brackets

  1. #1
    Registered User
    Join Date
    Mar 2002

    Question question: program on balancing brackets

    The below program has no compile or link error, but there is a possibility that there is logic error. Can you see any error? This is my assignment for data structure.
    The topic is somthing like this: ask user to key in a string, and see whether the brackets are balanced.

    /*Parentheses, brackets, and braces are symbols used to enclose
    expressions. For example, in arithmetic we may enclose an expression
    inside parentheses to indicate its higher precedence*/
    using namespace std;

    int main ()
    std::stack <char> braceStack;
    char firstChar,i;
    int top=-1;
    cout<<"Please enter characters of an mathematical expression with brackets to represent the order of operations being done(z to stop)\n";
    cout<<"enter the character one at a time(z to stop)...";
    if(i=='z') cout<<"The stack is empty.\n";


    else if (i==')'&& braceStack.empty()){
    cout<<"error! This expression is wrongly written\n";
    else if ((i==')'){
    braceStack.pop(); //Pop the top brace when they are compatible
    cout<<"Continue entering the character(z to stop)...";
    while (i!='z');

    cout<<"Error! This expression is wrongly written.\n";
    else if (braceStack.empty()&&firstChar!='z')
    cout<<"The brackets are balanced.\n";
    return 0;

  2. #2
    I'm Back
    Join Date
    Dec 2001
    i haven't seen the code but just keep count1 for ( and count2 for )
    and use if-else to chk.

  3. #3
    ¡Amo fútbol!
    Join Date
    Dec 2001
    Why do you need the std:: prefix to the declaration of the stack if you already say using namespace std.

  4. #4
    to ihsir and golfinguy4
    ihsir->the topic also include using "stack" to solve the prob.

    golfinguy4-> thanks dfor telling me, actually, i think i copy the statement from somewhere, and i also don't know what the statement " using namespace std" mean...i learnt writting "<iostream.h>", but i can't write "#include<stack.h>"
    but i can write "#include<stack>" together with "using namespace std"

    is it because of newer compiler don't support "#include<stack.h>"?

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Question regarding a this C program
    By cnb in forum C Programming
    Replies: 10
    Last Post: 10-11-2008, 04:43 AM
  2. newb question: probs with this program
    By ajguerrero in forum C Programming
    Replies: 5
    Last Post: 04-19-2006, 08:04 AM
  3. Random Question Assign Program
    By mikeprogram in forum C++ Programming
    Replies: 6
    Last Post: 11-17-2005, 10:04 PM
  4. Question type program for beginners
    By Kirdra in forum C++ Programming
    Replies: 7
    Last Post: 09-15-2002, 05:10 AM
  5. Replies: 8
    Last Post: 03-26-2002, 07:55 AM