Palindrome tester always tests as palindrome problem

  • (2 Pages)
  • +
  • 1
  • 2

15 Replies - 485 Views - Last Post: 10 March 2013 - 10:59 PM Rate Topic: -----

#1 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 04:49 PM

The word being tested always comes out as a palindrome. I cannot figure out why.
Any help is appreciated.Here's my code:


#include <iostream>
#include <string>
#include "Stack.h"
#include "Queue.h"

int main()
{
	Stack<char> s;
	Queue<char> q;
	bool palindrome = true;
	char input = ' ';
	
        //retrieve data from user
	cout <<"Enter the word you wish to test: ";

        //enqueue and dequeue word
	while(cin.get(input) && input != '\n')
	{
		q.enqueue(toupper(input));
		s.push(toupper(input));
	}

	// validate if palindrome
	while( (!q.isEmpty() && !s.empty()) && palindrome)
	{
		if(q.getFront(input) == s.getTop(input))
		{
			q.dequeue(input);
                        s.pop();
		}
		else
		{
			palindrome = false;
		}
	}

	// display results to the screen
	if(palindrome == false)
	{
		cout << input <<" is not a palindrome!\n";
	}
	else
	{
		cout<< input << " is a palindrome! \n";
	}

	return 0;
}




Is This A Good Question/Topic? 0
  • +

Replies To: Palindrome tester always tests as palindrome problem

#2 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 05:54 PM

Can you show us your implementation of Stack and Queue classes?
Was This Post Helpful? 0
  • +
  • -

#3 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,561
  • Joined: 19-February 09

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 05:56 PM

You shouldn't need to pass characters to these functions :

26        if(q.getFront() == s.getTop())
27        {
28            q.dequeue();


Was This Post Helpful? 0
  • +
  • -

#4 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 06:00 PM

You shouldn't... assuming a normal implementation of a queue and stack. That's why I asked to see the implementation in my previous post.
Was This Post Helpful? 0
  • +
  • -

#5 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 06:33 PM

Sure! Stack should be fine, but I found out the queue is the problem.
template <class DT>
class Queue
{
private:
	Node<DT> *front;
	Node<DT> *back;
	Node<DT> header;

public:
	Queue();
	~Queue( );
	void enqueue( const DT &);	
	bool dequeue( DT & );
        bool isEmpty( ) const;	
	void makeEmpty( );
	bool peek( DT & frontElement );  	
	bool getFront(DT &);

};

template <class DT>
Queue<DT>::Queue( )
{
	front = back = &header;
}


template <class DT>
Queue<DT>::~Queue( )
{
	makeEmpty( );
}

template <class DT>
void Queue<DT>::enqueue( const DT & element )
{
	Node<DT> *ptr = new Node<DT>;
	ptr->info = element;
	back->next = ptr;
	back = ptr;
}

template <class DT>
bool Queue<DT>::dequeue( DT & deqElement )
{
	if ( front == back ) 
		return false;

	Node<DT> *ptr = front->next;
	deqElement = ptr->info;
	front->next = ptr->next;
	if ( back == ptr )
		back = front;
	delete ptr;
	
	return true;
}

template <class DT>
bool Queue<DT>::peek( DT & frontElement )
{
	if ( front == back )
		return false;
	frontElement = front->next->info;
	return true;
}

template <class DT>
bool Queue<DT>::isEmpty( ) const
{
   	 return front == back;
}

template <class DT>
void Queue<DT>::makeEmpty( )
{
   	DT temp;
	while ( dequeue( temp ) );
}


template <class DT>
bool Queue<DT>::getFront(DT & item )
{
	bool found = false;
	if(!isEmpty())
	{
		found = true;
		item = back ->info;
	}

	return found;
}

#endif



*I know the problem is in the queue but dont know what the problem exactly is.
Was This Post Helpful? 0
  • +
  • -

#6 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,561
  • Joined: 19-February 09

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 08:12 PM

This returns a character in the parameter so you would need to call this before the if statement and check the returned character.


17	    bool getFront(DT &);



~

This post has been edited by #define: 10 March 2013 - 08:34 PM

Was This Post Helpful? 1
  • +
  • -

#7 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 08:35 PM

And probably a similar change for using the Stack as well.
Was This Post Helpful? 0
  • +
  • -

#8 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 08:42 PM

@#define i'm not sure im following. Could you elaborate please?
Was This Post Helpful? 0
  • +
  • -

#9 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 08:51 PM

Think of it this way. Your Queue::getFront() and Queue::getTop() both return true when successfully retrieve either the front or the top. Since you put in N items into each container, then you can always successfully get N items out (barring any coding errors or memory corruptions). Your if statement is comparing the true returned by getFront() against the true returned by getTop().

What #define is trying to tell you is that you should be comparing the values that you got from the front and the the top, not the return values of the functions.
Was This Post Helpful? 0
  • +
  • -

#10 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 08:59 PM

Do this is what I did for the queue:
template <class DT>
DT Queue<DT>::getFront(DT & item )
{
	if(!isEmpty())
	{
		item = back ->info;
	}

	return item;
}




and for the stack
template<class DT>
DT Stack<DT>::getTop(DT & item)
{
	if(!empty())
	{
		item = top->info;
	}

	return item;
}



But now it's always coming out as not a palindrome.
Was This Post Helpful? 0
  • +
  • -

#11 Skydiver  Icon User is offline

  • Code herder
  • member icon

Reputation: 3552
  • View blog
  • Posts: 11,008
  • Joined: 05-May 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 09:06 PM

Why do you need the reference parameter for those two methods?
Was This Post Helpful? 0
  • +
  • -

#12 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 09:11 PM

I guess I really don't, but I was doing it more for speed than anything else.
Was This Post Helpful? 0
  • +
  • -

#13 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 09:17 PM

Any ideas why now its coming back as not a palindrome?
Was This Post Helpful? 0
  • +
  • -

#14 #define  Icon User is online

  • Duke of Err
  • member icon

Reputation: 1330
  • View blog
  • Posts: 4,561
  • Joined: 19-February 09

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 10:28 PM

It looks like it is getFront :

template <class DT>
DT Queue<DT>::getFront(DT & item )
{
	if(!isEmpty())
	{
		item = back ->info;
	}

	return item;
}




It is called getFront but it gets from the back. I will let you have a go at correcting it.
Was This Post Helpful? 0
  • +
  • -

#15 sunnyxm  Icon User is offline

  • New D.I.C Head

Reputation: 1
  • View blog
  • Posts: 19
  • Joined: 24-August 12

Re: Palindrome tester always tests as palindrome problem

Posted 10 March 2013 - 10:31 PM

I already tried changing it to front -> info and still get the same problem.
Was This Post Helpful? 0
  • +
  • -

  • (2 Pages)
  • +
  • 1
  • 2