Use the stack class in a program that reads a String, one character at a time, and determine whether the String contains balanced parentheses, that is , for each left parenthesis ( if there any ) there is exactly one matching right parenthesis later in the String .

so the issue is

if (xx) => correct
if ((xx)) =>correct
if(((x) => false .....

so i managed to do a big part of it, i just need to fix this last part:

bool stack::balanced(string st)
{
    string strtmpl; // creating a string to hold the input in it
    strtmpl = st; //creating a string template to store the string entered by the user
    stack v; // overloading a stack

        int xx; // creating a variable to hold string length in it
        xx = strtmpl.length(); // filling variable xx with string length

        for(int z=0;z<=xx;z++)
        {
            v.puch(strtmpl[z]); //pushing the string into a stack
        }

        int i=0;
        int n =0;
        //cout<<i;
        //cout<<(char(v.StackArray[v.stacktop-1]));
        while(char(v.StackArray[i]) == '(')
        {
            if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(')
            {
                if (char(v.StackArray[v.stacktop-1]) == ')')
                {
                    n++;
                }
                v.pop();

                //cout<<char(v.StackArray[v.stacktop-1]);
                //i++;
                //xx--;
                //return 1;
            }
            //else
            //{
                //cout<<"bad!";
            //  n++;

                //xx--;
            //}
            i++;

        }
        if (n++ != i++)
                cout<<"bad";
            else if ((char(v.StackArray[v.stacktop-1])) != ')')
                cout<<"good";
        return 0;
}

Recommended Answers

All 6 Replies

People generally like a more specific question. What exactly is the problem?

i fixed it anyway yesterday :)
i really hate this about daniweb everytime i ask a question no one really helps, its like the answers i receive always"
i cant help you
i dont understand your question
this is too long to be asked on a forum
unlike 10 other different forums, daniweb is the worsest in helping this attitude is not present on any other forum...

anyway this is the solution for someone to benefit in the future if he was around the same problem:

bool stack::balanced(string st)
{
	string strtmpl; // creating a string to hold the input in it
	strtmpl = st; //creating a string template to store the string entered by the user
	stack v; // overloading a stack
	int flag =0;
		int xx; // creating a variable to hold string length in it
		xx = strtmpl.length(); // filling variable xx with string length

		for(int z=0;z<=xx;z++)
		{
			v.puch(strtmpl[z]); //pushing the string into a stack
		}

		int i=0;
		
		//while(char(v.StackArray[i]) == '(' || char(v.StackArray[xx]) == ')') you can iterate this way
		while(strtmpl[i] == '(' || strtmpl[xx-1] == ')') // or iterate this way does not mater
		{
			if ((char(v.StackArray[v.stacktop-1])) == ')' && strtmpl[i] == '(') //check if the begining of the string & the end contain ()
			{
				v.pop(); // remove from stack 		
				flag = 1; //success
			}
			else
			{
				if ((strtmpl[i] != '(') || (char(v.StackArray[v.stacktop-1]) != ')')) //
				{
					flag = 0; // failure
					break; //stop while
				}
			}
		i++; //iterate
		xx--; //iterate
		}
		if (flag ==1)//in case of success
			cout<<"Very good you parenthesized well :)"<<endl;
		else //in case of failure
			cout<<"Bad parethesis, please try again :("<<endl;

				return 0;
}
commented: Terrible attitude. -2
commented: leave, if you don't like it here -3

The beauty about stacks is that you can use them in so many ways to solve one problem; the simplest, would be to read a string in a loop and insert every '(' into a stack, while popping the stack once for every ')'. Under this logic, simply make the statement false if the stack is empty when you attempt to pop it, or if you still have a value in your stack when the statement ends.

i fixed it anyway yesterday :)
i really hate this about daniweb everytime i ask a question no one really helps, its like the answers i receive always"
i cant help you
i dont understand your question
this is too long to be asked on a forum
unlike 10 other different forums, daniweb is the worsest in helping this attitude is not present on any other forum...

anyway this is the solution for someone to benefit in the future if he was around the same problem:

Great way to never get helped here. Remind us again how much we're getting paid to help you. ALL forums, not just Daniweb, ask for clarification and effort by the posters. We're volunteering our time. Show some appreciation.

If you are the same person who posted here:

http://www.programmingtalk.com/showthread.php?p=134369

then Daniweb is not the only forum where people aren't willing to just do the whole thing and wrap it in a bow and deliver it to you.

this was in 12-01-07, 12:32 PM
well i didn't ask for anyone to do the H.W. for me, i just asked you people to check for the error in the if statement i made..

i am pro in another language & i help people all the time actually on another form, this is what programming forums are about,
if you like i can PM the forum i am talking about & you can see how friendly people are there & we actually do help each other.

thanks anyway, i appreciate the reply.

this was in 12-01-07, 12:32 PM

But is it you?

well i didn't ask for anyone to do the H.W. for me, i just asked you people to check for the error in the if statement i made..

No, you didn't mention anything about an if statement or where in the code the problem was and whether there were any compile errors, etc. You posted a function and said you had a problem with it. WE would have had to have figured out all of that. nucleon simply asked for a more structured question.

i am pro in another language & i help people all the time actually on another form, this is what programming forums are about,

I'm aware of what programming forums are about and lots of people here, including me, give tons of free help. You never gave anyone any opportunity to help you because you pulled an attitude right away. You had two threads on Daniweb so far asking for help, this one and an earlier one. In the first one, you apparently PM'd someone for help when you should have simply posted on the thread itself, so you got slammed a bit in the thread. In this one, you were simply asked for a more specific question. You are likely to get those responses on any forum.

if you like i can PM the forum i am talking about & you can see how friendly people are there & we actually do help each other.

Please do, along with your user name. I'd be interested in seeing it.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.