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 as for the actual palindrome detecting algorithm, I don't think your code will work. Instead, I think you need to use deques of chars. I'm not all that comfortable with deques, I've maybe used them once, but from my understanding of them this should work:

string input = "";
deque<char> Entry;
deque<char> Reversed;
getline(cin, input);// this allows you to get user input
//fill the deques
for (int i = 0; i < input.length(); i++)
{
    Entry.push_back(input[i]);
    Reversed.push_front(input[i]);
}
//check for equality...not sure if deque==deque would work?
bool bFlag = true;
for (int i = 0; i < input.length(); i++)
{
    if (Entry[i] != Reversed[i])
    {
          //not a palindrome
          bFlag = false;
          break;
    }
}
//if statements go here

And you said you weren't getting any output? This is probably because you need to pause the program before it closes:

//...Code...
cin.ignore(cin.rdbuf()->in_avail());
return 0;

Thank you for your reply. My assignment specifies that I have to use stacks.

I tried adding that extra line of code you suggested, but it didn't change anything. I am getting output but just the "enter text" and cin part. It doesn't output whether it is a palindrome or not.

Oops. You also need to put an empty cin.get() after that line:

cin.ignore(cin.rdbuf()->in_avail());
cin.get();
return 0;

The first line flushes the input buffer, so that it doesn't cause a false \n character (enter) to be read by cin.get(), which would normally happen after the user inputs data (in the previous cin.getline). Then the cin.get() just waits until the user hits enter.

StackOne is never empty, so your while loop won't stop...

StackOne is never changed inside the while loop...

I added the additional line of text, still no change. Something is obviously wrong with my approach.

Stack one is never empty? How can I change my program to make it empty? will this affect the output.

I am very inexperienced at this stuff, so thanks all for suggestions!

There are several problems with your program...

First of all, stackOne.front() doesn't change StackOne, you need to include StackOne.pop_front() or StackOne.pop_back() to change StackOne.

Also, StackOne==StackTwo will always be false, since StackOne is now empty... you might want to declare another StackThree as a copy of StackOne so you know what StackOne used to contain.

Finally, the stacks are stacks of strings, not stacks of chars. So you aren't taking individual letters off the input, you are taking the whole input off StackOne and putting it on StackTwo.

You shouldn't need to use deques... you can just use strings and let the first character of the new string be the last char of the first, etc., and then you get a palindrome and check whether those two strings are the same.

I understand the changes that you are suggesting, for the most part. I am not too clear on a couple things though.

1. I don't really understand what you mean by using pop_back or pop_front to "change the stack"

2. I don't know how to impliment the thing you were saying about the string and char's.

Would you mind helping me to implement these changes into my code?

Thanks a bunch!

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.

Well finally i got it .. i know nothing great in the program its just i am new to this style of programming.
Well the following is the code

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

int main ()
{
  string str ;
   int i =0;
   getline ( cin , str);
  string::reverse_iterator rit;
  string::iterator it;
  for ( rit=str.rbegin() ,it=str.begin(); rit < str.rend() && it < str.end(); rit++ ,it++)
  {  if( *rit!= *it)
    {cout<<" not palindrome";
        i=1;
        break;
  }

  }
if(i==0)
cout<<" palindrome";
  return 0;
}

well i tried it and it works fine . i would be glad if u could make it still better .

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...

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.

Well at the first place if ur assignment has only said palindrome program , then i find no reason to use stack . all u got to do is to iterate from first and last at the same time and check the characters . rather than pushing in some stack and then popping it all . u will have unnecessary extra code which can be reduced to a larger extend .

Sky Diploma,

Thank you for you suggestions. That seems like the route I want to take but can you help me to impliment this part:

1) create the stacks to fill in characters.
2) After getting the input. Run a for loop to send in each character into STACK1

Can you help me code this part, I can't figure out the right syntax?
Thanks so much.

Rahul,

Thank you for your reply, but the assignment specifies that I must use stacks.


SO basically this is what I need to do, correct me if I am wrong.

Create three stacks, and a string of characters for getting input.
fill the first stack with the input, copy it into the second stack, pop the first stack off onto the third stack, and compare the second and third stacks, right?

I think I understand the logical procedure, I just can't get the code right.

Well even if u have to use stacks i dont think u need three stacks .
Well u can do something like this .
just create one stack where the elements of the string is pushed.
then pop the elements of the stack and check with the elements of the string itself . if there s any kind of mismatch print not palindrome and break of .

Well the code which i have mentioned earlier works perfectly fine .
i can still write the stack version of it , the only problem is that my code is very traditional and kinda outdated (but still will work ) compared to this new style of programming.

Rahul,

I just tested your program and it works fine. Kudos to being able to just come up with it that fast. I am very new to this so I am still struggling a little bit with this assignment. I can't wait until I can do it that quickly. One thing I overlooked about this project is that at each point, you can only read one character of the input string, so I know I need to create a string of characters which will be input by the user to read into a stack one at a time. I need a total of three stacks I assume:
1. The first one that I copy the string of characters onto.
2. A second one to store the characters after they have been popped off LIFO (reversing the string).
3. A third stack to hold a copy of the first string because after you pop the characters off the first string that string will be empty.

my problem is that I cant figure out how to impliment it correctly in my code.
That is according to the instructions of the assignment. I also think that I need to create a third stack so one stack can hold a copy of the first stack.

well i have written the stack version of the code , but the code is in traditional sense , i guess you can convert it into ur style of programming .

#include<iostream.h>

char *stack;
int top=-1;
void push ( char c)
{
	stack[++top] = c;
}

char pop( )
{
return (stack[top--]);
}
int main ()
{
char *str;
int len,check=0,i;
cout<<" enter the length of string ";
cin>>len;
stack = str = new char[len];
cout<<"enter the string";
for( i=0;i<len;i++)
{cin>>str[i];
push(str[i]);
}


for (i=0;i<len;i++)
{

	if(str[i] != pop())
	 {
	     check=1;
	     cout<<"not palindrome";
	     break;
	 }
}

if (check == 0)
cout<<" palindrome";
return 0;
}

Well it functions like a stack n i have mentioned the push and pop actions also. the only trouble is that u will have to press enter after entering each character of the string .

Thank you Rahul. I appreciate you taking the time to help me out. I don't think I know how to convert that code to what I need. Heck, I don't even know how to write what I need, lol! Oh well I guess I need to do some more research!

what is a method I can use to copy the contents of one stack to another and can you give a coded example?

Thank you Rahul. I appreciate you taking the time to help me out. I don't think I know how to convert that code to what I need. Heck, I don't even know how to write what I need, lol! Oh well I guess I need to do some more research!

well its my pleasure , i am always fanatic about programming .
Well actually technically there isnt any thing called convert the code , its just that i had intially learned c++ in a traditional style which is outdated now (that does not mean that the code doesnt work ) , its just that the new way of programming delivers more Object oriented programming rather than the traditional one .


well technical the method here i have given is the one i have mentioned before .
the code works perfectly fine . nothing wrong as far as i know , but may be a bit amateur compared to the one i have given initially.

as i said before .

1. i have accepted the length of the string .
2. created a dynamic stack / space to hold the content of string .
3. as i am accepting the string i am alos pushing that into stack .
4. then i am running an iterative loop where i am checking the string from the beginning and also popping the elements from the stack .
5. if there exist any kind of mismatch ,i break of and print not a palindrome or else its an palindrome .

what is a method I can use to copy the contents of one stack to another and can you give a coded example?

well to the best of my knowledge (well i aint know much ) , u have actually used
1. "string input " in ur code , but instead if u declare a char array
who s size is dynamically allocated then we can handle the individual elements using the iterative loop .

2.there may exist any kind of function or operator in the header file ur using , u may check that too. which may make things far more easier .

well these are the two options i can currently think of .

This article has been dead for over six months. Start a new discussion instead.