Here's my code:

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

int main()
{
	string input;
	cout << "Enter text for palindrome checker.  No spaces or punctuation please." << endl;	
	getline(cin, input);

	stack<char> stackOne;
	for(char i = 0; i < input.length(); i++)
	stackOne.push(input[i]);

	stack<char> stackTwo;
	for(char j = 0; j < input.length(); j++)
		stackTwo.push(input[j]);

	stack<char> stackThree;
	while (!stackTwo.empty())
	


	return 0;
}

My question is : what code can I add to push the contents of stack two onto stack three?

Thanks

stackThree.push(stackTwo.pop());

Note that the elements will be in reverse order. If you want the same order, you should use a queue or an array (or another thing that keeps the stack content).

An operator=() may exist, too.

Okay, my code now looks like this:

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

int main()
{
	string input;
	cout << "Enter text for palindrome checker.  No spaces or punctuation please." << endl;	
	getline(cin, input);

	stack<char> stackOne;
	for(char i = 0; i < input.length(); i++)
	stackOne.push(input[i]);

	stack<char> stackTwo;
	for(char j = 0; j < input.length(); j++)
		stackTwo.push(input[j]);

	stack<char> stackThree;
	while (!stackTwo.empty())
		stackThree.push(stackTwo);

if( !stackOne.empty() && !stackTwo.empty() )
{
       if(stackOne.top() == stackTwo.top() )
	   	stackOne.pop();
		stackTwo.pop();
	   else
		   cout << "It is not a palindrome.  \n";
}
else
cout << " It is a palindrome.  \n";



	return 0;
}

The line that says "stackThree.push(stackTwo); " was my attempt push the contents of stack two onto stack three.

I get this error:

c:\Users\Lauren\Documents\Visual Studio Projects\Palindrome\main.cpp(22) : error C2664: 'std::stack<_Ty>::push' : cannot convert parameter 1 from 'std::stack<_Ty>' to 'const std::stack<_Ty>::value_type &'


So what am I doing wrong? How can I fix this so that stack three will hold the contents of stack two (backwards using push())?

stackThree.push(stackTwo.pop());

Note that the elements will be in reverse order. If you want the same order, you should use a queue or an array (or another thing that keeps the stack content).

I want them in reverse order so I can compare each top element and see if it is a palindrome.

You don't want to push another stack, you want you push an element of the other stack, so call pop(), like this:

stackThree.push(stackTwo.pop()); //This will push the elements in reverse order.

I also suggest that you change the "char" type as an "unsigned int" to eliminate the warning at lines 13 and 17.

oh and I am also getting "illegal else without matching if" both of my else's have matching if's. What to do about that?

I'm sorry. pop() removes the first element without returning it (if you're using std::stack). top() returns the value of the top, and pop() removes the top. You should use top() with push() and do pop() on the source stack after.

You are having an illegal else because you surely have forgotten the brackets of the if clause, at lines 27 and 28. Currently, the code executed if the "if" statement is true is stackone.pop(). In all cases, stackTwo.pop() is executed, then the compiler finds the else and it complains, so it's the correct behavior of the compiler.

Thanks for the reply. I'll try making those changes.

No problem. Let me know if you need more help.

Okay, here is what I have now:

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

int main()
{
	string input;
	cout << "Enter text for palindrome checker.  No spaces or punctuation please." << endl;	
	getline(cin, input);

	stack<char> stackOne;
	for(char i = 0; i < input.length(); i++)
	stackOne.push(input[i]);

	stack<char> stackTwo;
	for(char j = 0; j < input.length(); j++)
		stackTwo.push(input[j]);

	stack<char> stackThree;  
	while (!stackTwo.empty())
	{
		stackTwo.top() = i;
		stackThree.push(i);
		stackTwo.pop();
	}


if( !stackOne.empty() && !stackTwo.empty() )
{
       if(stackOne.top() == stackTwo.top() )
	   {
	   	stackOne.pop();
		stackTwo.pop();
	   }
	   else 
	   {
		   cout << "It is not a palindrome.  \n";
	   }
}
else 
{
	cout << " It is not a palindrome.  \n";
}
	return 0;


}

Almost works fine, except it always displays "It is not a plaindrome" and isn't getting to the "Is a paindrome" part of the loop. Do you have any suggestions that can help.

Thanks again!

I just realized I typed "It is not a palindrome" twice. My bad!

I'm glad that you found the error yourself!

Don't forget to mark the thread as solved.

I fixed that little typo and now all I am getting is "It is a palindrome!"

So it's actually not getting to the "It's not a palindrome" part.

I have tried removing some of the braces, it didn't work.

The last "It is not a palindrome " is actually "It is a palindrome"

Do you have any suggestions?

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.