Can anyone tell me what I am doing wrong in this code? The output window allows me to input the string, but does not output whether or not it is a palindrom. There are no build errors
Thanks in advance!
Lauren
#include <iostream>
#include <deque>
#include <string>
#include <cctype>
using namespace std;
int main()
{
string input;
deque<string> stackOne;
deque<string> stackTwo;
cout << "Enter text to see if it is a palindrome. Please do not include spaces or punctuation.\n";
getline(cin, input);// this allows you to get user input
stackOne.push_front(input);
while(!stackOne.empty()) // this part reads stackOne into stackTwo.
{
input = stackOne.front();//retrieve the user entered input
stackTwo.push_front(input); // put input into stack two at the front
}
if(stackOne == stackTwo)
{
cout << "It is a palindrome." << endl;
}
else
cout << "It is not a palindrome." << endl;
return 0;
}
Also, any suggestions on how I can change this code so that the string is read into the two stacks one character at a time, so that I can compare them that way. And just to let you know, yes this is a homework assignment. I just need help getting a better sense of direction. THanks again
Oh, and one more thing, I can't use an array
Well i dont get the first code over here.
You are getting a string totally using getline and putting it into a stack.
Then putting back the same string into stack2.
I think you should do the following.
FIrst
1) create the stacks to fill in characters.
2) After getting the input. Run a for loop to send in each character into STACK1
3) Make sure if this code
if(stackOne == stackTwo)
{
cout << "It is a palindrome." << endl;
}
else
cout << "It is not a palindrome." << endl;
return 0;
}
Gives you the desired output.
4)The WHile loop should be like this
while(!stackOne.empty()) // this part reads stackOne into stackTwo.
{
input = stackOne.front();//retrieve the user entered input
stackOne.pop_front();//Removes the first character.
stackThree.push_back(input);
stackTwo.push_front(input); // put input into stack two at the front
}
After all this i dont think you will need to change your program.
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131
You still need to create a StackThree as a copy of StackOne, since now StackOne will be empty so StackOne = StackTwo will clearly be false, but other than that the program should work...
True, SO either use a loop to get to the end of the stack or you should create a third queque and copy your first stack there.
Sky Diploma
Practically a Posting Shark
865 posts since Mar 2008
Reputation Points: 673
Solved Threads: 131