I am supposed to make a palindrome program using stacks, but my function keeps returning false. Here's the code:

Main:

int main()
{
	char word[50];

	cin >> word;

	cout << strlen(word);

	if (checkIfPalindrome(word) == true) {
		cout << endl << "Is a palindrome.";
	}
	else
	{
		cout << endl << "Not palindrome.";
    }

	getch();
}

bool checkIfPalindrome(char word[])
{
	charStack reverseWord;
	bool fail;

        reverseWord.top = strlen(word);

	for (unsigned int i = 0; i < strlen(word); i++)
	{
		reverseWord.push(word[i]);
	}
	cout << endl << reverseWord.array << endl;
	for (unsigned int j = 0; j < strlen(word); j++)
	{
		if (word[j] != reverseWord.pop())
		{
			return false;
		}
	}
	return true;
}

Definition of push and pop:

int charStack::push(char item)
{
	if (!full()) {
		array[top++] = item;
	}
	else
	{
		return -1; //Error
	}
	return 0; //Success
}

char charStack::pop()
{
	char deletedItem;

	if (!empty()) {
		deletedItem = array[top];
		top--;
	}
	else
	{
		return -1; //Error
	}
	return deletedItem;
}

bump

commented: Do NOT bump threads. It's rude and useless. When someone can help, they will. -3

You haven't posted all of your code, so it's hard to know what all your problems are. However, the code that you posted for charStack::push ahd charStack::pop is broken. To see this, try creating a charStack object, pushing a few characters onto it, and then popping them one at a time and printing them. What you get should, of course, be the same as what you pushed, just in reverse order; but that's not what your code actually does.

Ah, thanks. In the pop function, top was supposed to be top - 1.

Yikes, is that really Andrew Koenig or a very good Impersonator!!!

Yikes, is that really Andrew Koenig or a very good Impersonator!!!

I'm not impersonating anyone--but it's not exactly obvious how I might go about proving it if you don't already know me. If you do, I could tell you something about myself that an impersonator would be unlikely to know--but if you don't know me, you also don't necessarily know how to distinguish what I tell you from information that might be publicly available.

If you do an online search for me, you will find lots of hits, both for me and for the actor with the same name who vanished last year in Vancouver and was later found dead. There's no relation between us that I know, although we had been friends on Myspace.

You'll find lots of copies of the photo I use for my avatar, too, so an impersonator could have snarfed that from anywhere. I can tell you lots of things about the kitten that was on my shoulder when that picture was taken, but of course I could be making them up.

So yes, it's really me; but I can't think of a convenient way to prove it.

I can think of a somewhat inconvenient way to prove it. Pick up a copy of Accelerated C++ and look at the preface, which tells where I live. Look me up in your phone directory of choice--my phone number is listed. Call it, and give me a piece of information that you'd like me to post in this forum.

The trouble with this technique is that it proves it only for the person who phoned me. Other people still haven't ruled out the possibility that I'm an impostor who colluded with the individual who claims to have phoned me, or that that person isn't just making up the whole story.

I'd be willing to chat with one of the moderators at a mutually convenient time if there's one who cares enough to establish my identity on this list. I'd rather not do it for every curious reader, though.

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.