I am trying to create a palindrome checker using a single stack and pass by reference method. I just have to write the evalPalindrome() method and then I input it into a web application which holds the main. For some reason, however, when the main is calling the pass by reference reversePhrase, either nothing is being passed or it is not recongizing it because there is no output for it. Do I need to delcare this differently in the Palindrome.cpp?

The reversePhrase should output with all the spaces and punctuation first, then all the punctuation should be removed and put back into a string. So this way only the letters should be compared. Am I performing this function correctly?

Please shed some light on this. Thank you.

The code for the stack is as follows (I pulled it straight from the book I am using to learn):

//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** @file StackInterface.h */

#ifndef _STACK_INTERFACE
#define _STACK_INTERFACE

template<class ItemType>
class StackInterface
{
public:
   /** Sees whether this stack is empty.
    @return True if the stack is empty, or false if not. */
   virtual bool isEmpty() const = 0;

   /** Adds a new entry to the top of this stack.
    @post If the operation was successful, newEntry is at the top of the stack.
    @param newEntry The object to be added as a new entry.
    @return True if the addition is successful or false if not. */
   virtual bool push(const ItemType& newEntry) = 0;

   /** Removes the top of this stack.
    @post If the operation was successful, the top of the stack
    has been removed.
    @return True if the removal is successful or false if not. */
   virtual bool pop() = 0;

   /** Returns the top of this stack.
    @pre The stack is not empty.
    @post The top of the stack has been returned, and
    the stack is unchanged.
    @return The top of the stack. */
   virtual ItemType peek() const = 0;
}; // end StackInterface
#endif



//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** ADT stack: Array-based implementation.
 Listing 7-1
 @file ArrayStack.h */

#ifndef _ARRAY_STACK
#define _ARRAY_STACK

#include "StackInterface.h"

const int MAX_STACK = 50;

template<class ItemType>
class ArrayStack : public StackInterface<ItemType>
{
private:    
    ItemType items[MAX_STACK]; // Array of stack items
    int      top;              // Index to top of stack

public:
     ArrayStack();             // Default constructor
     bool isEmpty() const;
     bool push(const ItemType& newEntry);
     bool pop();
     ItemType peek() const; 
}; // end ArrayStack

#include "ArrayStack.cpp"
#endif




//  Created by Frank M. Carrano and Tim Henry.
//  Copyright (c) 2013 __Pearson Education__. All rights reserved.

/** Listing 7-1
    @file ArrayStack.cpp */
#include <cassert>       // For assert
#include "ArrayStack.h"  // Header file

template<class ItemType>
ArrayStack<ItemType>::ArrayStack() : top(-1)
{
}  // end default constructor

// Copy constructor and destructor are supplied by the compiler

template<class ItemType>
bool ArrayStack<ItemType>::isEmpty() const
{
    return top < 0;  
}  // end isEmpty

template<class ItemType>
bool ArrayStack<ItemType>::push(const ItemType& newEntry)
{
    bool result = false;    
    if (top < MAX_STACK - 1)  // Does stack have room for newEntry?
    {
        top++;
        items[top] = newEntry;
        result = true;
    }  // end if

    return result;
}  // end push


template<class ItemType>
bool ArrayStack<ItemType>::pop()
{
    bool result = false;
    if (!isEmpty())
    {
        top--;
        result = true;
    }  // end if

    return result;
}  // end pop


template<class ItemType> 
ItemType ArrayStack<ItemType>::peek() const
{
    assert(!isEmpty());  // Enforce precondition

    // Stack is not empty; return top
    return items[top];
}  // end peek
// End of implementation file.

Here is my code that needs help:

/**
 * @file Palindrome.h
 */
#include <string>
#include <cctype>
#include "ArrayStack.h"
using namespace std;
class Palindrome
{
private:
   /**
    * stores the letters in the string while it is being evaluated
    * to determine if it is a palindrome
    */
   ArrayStack<char> letters; 

   /**
    * original phrase to evaluate to determine if it is a palindrome
    */
   string phrase;

public:
   /**
    * Default constructor. Initializes expression to an empty string.
    */
   Palindrome () 
   {
      phrase = "";
   }


   /**
    * Overloaded constructor that initializes the phrase.
    * @param - newPhrase - original phrase tested to determine if it is a 
    *          palindrome
    */
   Palindrome (string newPhrase) 
   {
      phrase = newPhrase;
   }

   /**
    * Evaluates the phrase to determine if it is a palindrome. Uses
    * a stack to reverse the order of the letters in the phrase and
    * to determine if the original phrase is a palindrome. A palindrome
    * is a sequence of letters that reads the same forward and backward;
    * however, all spaces and punctuation are ignored.
    * @return isPalindrome - true if phrase is a palindrome; false
    *                        otherwise
    * @param reversePhrase - orginal phrase in reverse order, including
    *                        all puncutation and spaces
    */
   bool evalPalindrome (string& reversePhrase); 
};



#include <cstdlib>
#include <iostream>
#include <string>
#include <ctype.h>
#include "Palindrome.h"

 /**
    * @file Palindrome.cpp
    * Evaluates the phrase to determine if it is a palindrome. Uses
    * a stack to reverse the order of the letters in the phrase and
    * to determine if the original phrase is a palindrome. A palindrome
    * is a sequence of letters that reads the same forward and backward;
    * however, all spaces and punctuation are ignored.
    * @return isPalindrome - true if phrase is a palindrome; false
    *                        otherwise
    * @param reversePhrase - orginal phrase in reverse order, including
    *                        all puncutation and spaces
    */
   bool Palindrome::evalPalindrome (string& reversePhrase)
   {
     char ch;
     int length = phrase.length();
     int i = 0;
     int j = 0;
     int k = 0;
     bool isPalindrome;
     int revLength = reversePhrase.length(); 
     string tempRev = " ";
     string tempOrig = " ";

        while(i < length)
        {
          ch = phrase[i];
          letters.push(ch);
          i++;          
        }//end while

        while(!letters.isEmpty())
        {
          reversePhrase[j] = letters.peek();
          letters.pop();
          j++;
        }//end while

        while(i < length)
        {
           if(isalpha(phrase[i]) && phrase[i] != ' ')
           {
              char c = phrase[i];
              tempOrig += toupper(c);
           }
        }//end while

        while(i < revLength)
        {
           if(isalpha(reversePhrase[i]) && reversePhrase[i] != ' ')
           {
              char d = reversePhrase[i];
              tempRev += toupper(d);
           }
        }//end while

        for(k = 0; k <= j; k++)
        {
           if(tempOrig[k] == tempRev[k])
           {
              isPalindrome = true;
           }
           else
           {
              isPalindrome = false;
           }
        }

   }

I just have to write the evalPalindrome() method and then I input it into a web application which hold the main. For some reason however when the main is calling the pass by reference reversePhrase, either nothing is being passed or it is not recongizing it because there is no output for it. The reversePhrase should putput with all the spaces and punctuation first, then all the punctuation should be removed and the letters only should be compared. I'm not sure if I am doing this correctly.
Please shed some light on this. Thank you.

Just noticed this post, that seems to go with your other post ...

Again, it would seem best to use the C++ STL stack (or list) to hold the char's

This could serve as a 'quick/cheap' test program ...
(and your eval... section, could be vastly cleaned-up/simpified.)

// testPalindrome.cpp //

#include <iostream>

// quick/cheap test method ... //

#include "palindrome.cpp"
// includes all the below ...
/*
#include "palindrome.h"

#include <string>
#include <cctype>

#include <algorithm>

//#include "ArrayStack.h"

#include <stack>

using namespace std;
*/



int main()
{
    string test = "a b c d e f g f edcba", revStr;

    cout << test << endl;

    Palindrome p( test ); // construct ...

    cout << ( p.evalPalindrome( revStr ) ? "true" : "false" )  << endl;

    cout << revStr << endl;

    cout << "Press 'Enter' to continue/exit ... " << flush;
    cin.get();
}

You may get some ideas from the example below ...
(that uses C++ STL stack and ...)

Recall that a stack is FILO <-> First In Last Out.

// testPalindromeAllInOne.cpp //

#ifndef PALINDROM_H
#define PALINDROM_H

/**
 * @file Palindrome.h
 */
#include <string>
#include <cctype>

#include <algorithm>

//#include "ArrayStack.h"

#include <stack>


using namespace std;


class Palindrome
{
private:
   /**
    * stores the letters in the string while it is being evaluated
    * to determine if it is a palindrome
    */
   stack < char > letters;

   /**
    * original phrase to evaluate to determine if it is a palindrome
    */
   string phrase;

public:
   /**
    * Default constructor. Initializes expression to an empty string.
    */
   Palindrome ()
   {
      phrase = "";
   }


   /**
    * Overloaded constructor that initializes the phrase.
    * @param - newPhrase - original phrase tested to determine if it is a
    *          palindrome
    */
   Palindrome (string newPhrase)
   {
      phrase = newPhrase;
   }

   /**
    * Evaluates the phrase to determine if it is a palindrome. Uses
    * a stack to reverse the order of the letters in the phrase and
    * to determine if the original phrase is a palindrome. A palindrome
    * is a sequence of letters that reads the same forward and backward;
    * however, all spaces and punctuation are ignored.
    * @return isPalindrome - true if phrase is a palindrome; false
    *                        otherwise
    * @param reversePhrase - orginal phrase in reverse order, including
    *                        all puncutation and spaces
    */
   bool evalPalindrome (string& reversePhrase);
} ;

#endif



/////////////////////////////////////////////////////////////////////
// palindrome.cpp //

// #include "palindrome.h"

bool Palindrome::evalPalindrome (string& reversePhrase)
{
    // get exact reversed copy of string ...
    reversePhrase = phrase;
    reverse( reversePhrase.begin(), reversePhrase.end() );

    // get FIXED UP copy of string into a stack ...
    for( size_t i = 0; i < phrase.size(); ++ i )
    {
        if( isalpha(phrase[i]) ) letters.push( phrase[i] );
    }


    size_t i = 0;
    while( i < phrase.size() )
    {
        while( i < phrase.size() && !isalpha(phrase[i]) )
            ++i;

        if( i == phrase.size() ) break;

        if( phrase[i] != letters.top() )  return false;

        letters.pop(); // pop and increment i
        ++i;
    }

    // check end condition ...
    if( letters.empty() &&  i == phrase.size() ) return true;
    // else ...
    return false;
}
//////////////////////////////////////////////////////////////////////



#include <iostream>


int main()
{
    string test = "a b c d e f g f edcba1", revStr;

    // show string ...
    cout << test << endl;

    Palindrome p( test ); // construct ...

    cout << ( p.evalPalindrome( revStr ) ? "true" : "false" )  << endl;

    // show reversed string
    cout << revStr << endl;

    cout << "Press 'Enter' to continue/exit ... " << flush;
    cin.get();
}
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.