Hi, I'm writing a program that checks parens/brackets/braces if they are paired. Tha catch is I have to write my own stack functions, this is my code so far, it used to work when I was working with just the parentheses, now that I added more options it's not working and now I'm lost, any expert advice or tips from a second pair of eyes? Thanks in advance.

BTW, did I write my initialize() and top() functions correctly?

Thanks again!

	This little program checks to see whether the left and right 
	parentheses / brackets / braces
	characters are properly mathed and nested in a line of text.

#include <iostream>
#include <string>
using namespace std;

const unsigned MAX_DEPTH = 4;

struct charStack {
	char *paren;
	unsigned elements; 
}	stack;

bool die		( const string& );
void initialize ( charStack& );
bool full		( const charStack& );
bool empty		( const charStack& );
void push		( charStack&, char );
char pop		( charStack& );
char top		( charStack& );
bool isOpen		(char); 
char isClose	(char);
bool balance	( charStack& );

int main () {
	string line;
	bool ok = true;
	initialize ( stack );
	cout << "Type a string: ";
	if ( !getline ( cin, line ) ) die ( "can't input string." );
	for ( unsigned i = 0; i < line.size(); i++ ) {
		if ( isOpen(line[i]) ) 
			push( stack, line[i] ); 
		else if ( line[i] = isClose(line[i]) ) { 
			if ( empty ( stack ) ) { 
				ok = false;
			else if (line [i] = top ( stack ))
				pop ( stack ); 
			else { 
				ok = false;

	if (!ok || (!empty ( stack ) ) )
		cout << "These parens don't match." << endl;
		cout << "These parens match. HOORAY!!" << endl; 


bool die ( const string & msg ) {
	cerr << endl << "Fatal Error: " << msg << endl;
	exit ( EXIT_FAILURE );

void initialize ( charStack & stack ) {
	char *newParens;
	newParens = (char *) malloc(sizeof(char) * MAX_DEPTH);
	if (newParens == NULL) {
		die ( "Insufficient memory to initialize stack." );
	stack.paren = newParens;

bool full ( const charStack & stack ) {
	if ( stack.elements == MAX_DEPTH - 1)
		return true;
	return false;

bool empty ( const charStack & stack ) {
	if ( stack.elements == 0 )
		return true;
	return false;

void push ( charStack & stack, char c ) {
	stack.paren[stack.elements++] = c;
	if(stack.elements > MAX_DEPTH) die ("Stack Overflow");

char pop ( charStack & stack ) {
	if(stack.elements < 0) die ("These parens don't match.");
	return (--stack.elements);

char top ( charStack & stack ) {
	if ( stack.elements == 0 )
		return 0;
	return ( stack.elements - 1);

bool isOpen (char c) {
	return ( c == '(' || c == '[' || c =='{' );

char isClose(char c) {
	switch (c) {
	return '\0';

now that I added more options it's not working and now I'm lost, any expert advice or tips from a second pair of eyes?

My advice is roll back to something that works and add new options more carefully. For example, add debugging output such as the contents of the stack and the values you're testing at each step.

I agree with Narue's advice, if it is possible - if you don't have a backup of the earlier version, and aren't using version control, it will be much harder to undo the changes. However, I was wondering if your course of study has covered classes yet; given the number of functions needed for the stack structure, it would make sense to wrap it all up into a class.

We covered a basic class lessons last semester. I will give that a try. Question, where is the debug output option? It seems that my bug is somewhere in the for loop at main.

My eagle eye has spotted this at line 43:

else if (line [i] = top ( stack ))

Are you sure you want a single equal sign there? I'm pretty sure you don't.

If I had a dime for every time I see this, I'd be a rich man.

Also at line 99, what are you returning from top()? And what did you mean to return? Also, you have an assignment instead of equality-test at line 38, but I don't know what the expression is supposed to do. A close-character will never equal its open-character, and any other non-null character will ever equal the null-character. Or if you're replacing each close-character with its corresponding open-character, and all other chars with null-characters, and proceeding into the if-block only for paren/bracket/brace, that's fine, but it's not exactly human-readable. :)

Maybe instead, you could consider separate functions bool isClose(char) and char openCharForCloseChar(char) , and use those in your logic without replacing the actual characters in line[].

I will try those advices. BTW, it seems that my top() is not working properly. Its function is to show the top on the stack, what's the best way to write it or to improve my code? THanks!

Well, what holds the data for the stack? And where is the top element in that data? I'll post the correct code in a couple hours (if I remember), but see if you can get it first! :)

I'm sorry I don't fully understand your question. Well what holds the stack is the stack.parens[stack.elements] if that's what you meant?

Oh I got it now, my program is now bug-free. Thanks for the help guys.