Hi, I am new to stacks and need some help getting started with my first program in stacks. I have all the functions like pop,push etc. in my header file. I have to create a palindrome program with the objectives below. If someone can give me an idea on how to start this program, I appreciate it. I added a main.cpp of a palindrome program that I did using recursion if that will give me a push on what I need to do. Also, do you declare a stack like this?

stack u;
stack r;
stack s;

Objectives:

1. As a line is read, push each character on a stack(s) and stack(u).
2. Transfer the info from stack(u) to stack(r) so that stack(r) is the reverse of stack(u)
3. Repeatedly pop elements from stack(s) and stack(r) to test equality
4. If equal, it prints "is a palindrome", if almost equal, it prints "is almost a palindrome, if not a palindrome, it prints "is not a palindrome"


Expected Output:

a b c d e f g h i j k l m n o p q r s t u v w x y z is not a palindrome
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 is not a palindrome
a b b a c c a c c a b b a is a palindrome
1 2 3 3 2 1 2 3 3 2 1 is a palindrome
1 2 3 2 1 2 4 2 1 is almost a palindrome


Input File:

a b c d e f g h i j k l m n o p q r s t u v w x y z
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
a b b a c c a c c a b b a
1 2 3 3 2 1 2 3 3 2 1
1 2 3 2 1 2 4 2 1

main.cpp

#include <fstream>
#include <string>
#include "stackarray.h"


using namespace std;

bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData); 
int main() 
{ 
string words; 


ifstream inData("input.txt"); 
ofstream outData("output.txt"); 


while ( getline( inData, words ) ) 
{ 
Palindrome( words, 0, words.length(),outData ); 
}

inData.close(); 
outData.close(); 

system("PAUSE");
return 0;
}
bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData)
{ 
if((paliLength == 0 || paliLength == 1)) 
{ 
outData << pali << " is a palindrome." << endl; 
return true;
}
if( pali[paliIndex] == pali[paliLength - 1] ) 
{
return Palindrome(pali,paliIndex+1,paliLength-1… 
}
else
{
outData << pali << " is not a palindrome." << endl; 
return false;
}
}

.h file

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{

public:
const stackType<Type>& operator=(const stackType<Type>&);
void initializeStack();
bool isEmptyStack();
bool isFullStack();
void destroyStack();
void push(const Type& newItem);
Type top();
void pop();
stackType(int stackSize = 100);
stackType(const stackType<Type>& otherStack);
~stackType();


private:
int maxStackSize;
int stackTop;
Type *list;
void copyStack(const stackType<Type>& otherStack);
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
delete [] list;
maxStackSize = otherStack.maxStackSize;
stackTop = otherStack.stackTop;

list = new Type[maxStackSize];
assert(list != NULL);

for(int j = 0; j < stackTop; j++)
list[j] = otherStack.list[j];

}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
list = NULL;

copyStack(otherStack);

}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
if (this != &otherStack)
copyStack(otherStack);

return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
if(!isFullStack())
{
list[stackTop] = newItem;

stackTop++;
}

else
cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
assert(stackTop != 0);

return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
if(!isEmptyStack())
{
stackTop--;
}
else
cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
if(stackSize <= 0)
{
cerr<<"The size of the array to hold the stack must "
<<"be positive."<<endl;
cerr<<"Creating an array of size 100."<<endl;

maxStackSize = 100;

Is there any hints or advice someone can please give me to help me start it up. This is my first time using stacks in a program.

First, you declare a stack like stack<char> u; .

Second, with the code your provided, you will never know if the word is almost a palindrome. The code relies on true means it is a palindrome and false means it isn't. There is no option for it being one letter off.

I would honestly just write it the way it's stated. I was able to do so rather quickly. It would look like:

while (/*get word to compare*/)
      { while () {/*pushes characters of word into stacks u and s*/}
        while () {/*pushes the top of u into r and then pop from u*/}
        while () {/*compares top elements, does something if the two not equal, pop elements*/}
        /*based on above, print appropriate output*/
      }
return 0;

A few suggestions, use stack<string> for your stacks because those numbers (10-26) will be broken into individual character otherwise and really mess up your code. Also, check out stringstream. It allows you to manipulate strings like they were input and it will make your job much easier. For example, the following code will print each word on it's own line.

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

int main() {
stringstream ss;
string word;
string line = "This is a sentence.";
ss << line;  // put line into stringstream
while (ss >> word)  // is basically like writing "cin >> word"
      { cout << word << endl; }
ss.clear();  // clears stringstream to get it ready for next string
system("pause");
return 0;
}

That should help get you started. If you need any more help, just ask.

How do I declare push and pop? I thought it was already declared in my .h file above?

#include <fstream>
#include <string>
#include <stack>
#include "stackarray.h"


using namespace std;

bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData); // uses boolean to return value

int main() 
{    
     string words; // name of my identifier used to read in data
     stack<string> u;
     stack<string> r;
     stack<string> s;
    
     ifstream inData("input.txt");  // declaration of inData
     ofstream outData("output.txt");  // declaration of outData
    
 
     while ( getline( inData, words ) )	// reads words in file using getline to capture all spaces
     {
        push.stack(u);
        push.stack(s);
        push.top(u);
        stack(r) = stack(u);
        pop.stack(u);
        
           	  	
        Palindrome( words, 0, words.length(),outData ); // function call	
     }
    
     inData.close(); // closes inData
     outData.close();  // closes outData
     
     system("PAUSE");
     return 0;
}
bool Palindrome (string pali, int paliIndex, int paliLength, ofstream& outData)
{      
  if((paliLength == 0 || paliLength == 1))  // compares length == 0 and length == 1
    {            
      outData << pali << " is a palindrome." << endl; // if above is true, pali is a palindrome
      return true;
    }
  if( pali[paliIndex] == pali[paliLength - 1] ) // compares index == length - 1
    {
      return Palindrome(pali,paliIndex+1,paliLength-1,outData); // If so, returns results
    }
  else
    {
      outData << pali << " is not a palindrome." << endl;  // If not, pali is not a palindrome
      return false;
    }
}

Do you think it is easier using my Palindrome program that uses iteration?

You technically don't even need the header file to use a stack. As long as you put #include <stack> at the top, it knows to use those functions.

And you don't declare those functions, they are already predefined, you simply call them by writing [i]stackname[/i].push(/*what you want to push into stack*/); and replace stackname with u, r, or s. Pop is the same way [i]stackname[/i].pop(); You should take a look at stacks to get used to them. http://www.cplusplus.com/reference/stl/stack/


Personal I didn't like your Palindrome function, but that's just because I'm used to writing exactly what the instructions say. Plus, it doesn't do what the instructions say so I don't think you would get credit for it.

I'm deleting my palindrome function and start from scratch. Stacks is just a new thing to me, and I thought I could build on top of my function. It does seem like it is easier if I started from scratch though. Do I need to use the same comparisons that I did in my Palindrome function for it to work or can it work strictly by pushing and popping?

This is what I have so far. However, when I compile program, the output box just comes and goes real fast. Do you know a reason why the output screen would come up and disappear?


main.cpp

#include <fstream>
#include <string>
#include "stackarray.h"


using namespace std;


int main() 
{    
     string words; // name of my identifier used to read in data
     stackType<string> u;
     stackType<string> r;
     stackType<string> s;
     
    
     ifstream inData("input.txt");  // declaration of inData
     ofstream outData("output.txt");  // declaration of outData
    
 
     while ( getline( inData, words ) )	// reads words in file using getline to capture all spaces
     {
        u.push(words);
        s.push(words);
        u.top();
        u.pop();
        if ( s.top() == r.top() )
        {
        s.pop();
        r.pop();
        outData<<words<<" is a palindrome";
        }
        else
        s.pop();
        r.pop();
        outData<<words<<" is NOT a palindrome";
        
           	  	
     }
    
     inData.close(); // closes inData
     outData.close();  // closes outData
     
     system("PAUSE");
     return 0;
}

.h file

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}


#endif

I'm also thinking about using a for-loop after the while loop. Do you think that is a good idea?

I'm not sure why it is disappearing so quickly, but one way is to add getchar(); to the code before return zero. Some compliers don't understand system("pause"); so this is a way to get it to stop. All you do to get past it is to hit any key.

I suggest you remove the header file. There is already a stack file built into the complier so you're basically re-making the wheel by using your own.

I wrote the code, so obviously I think it's good, but I'd highly suggest you follow this outline. It's how I rewrote mine to fit your specifications and it worked fine.

#include <iostream>
#include <fstream>
#include <string>
#include <stack>

using namespace std;

int main() 
{    
     string line; // name of my identifier used to read in data
     string word; // individual characters
     stack<string> u;
     stack<string> r;
     stack<string> s;
    
     ifstream inData("input.txt");  // declaration of inData
     ofstream outData("output.txt");  // declaration of outData
    
 
     while ( getline( inData, line ) )	// reads words in file using getline to capture all spaces
           { /*something to break up line into individual character*/
             while (/*there are still characters*/) 
		   {/*pushes characters of word into stacks u and s*/}
             while (/*u is not empty*/) 
		   {/*pushes the top of u into r and then pop from u*/}
             while (/*s is not empty*/) 
		   {/*compares top elements, does something if the two not equal, pop elements*/}
             /*based on above, print appropriate output*/           	  	
           }
    
     inData.close(); // closes inData
     outData.close();  // closes outData
     
     system("pause");
     return 0;
}

Thank you for your reply. This is what I currently have. I am receiving that isEmptyStack(): is not declared, but it is in my stackarray.h file. Even though it is a function, do I need to declare it above main?

Also, I don't know if I did these correctly, or I don't know if it typed it correctly. Please check my work.

/*pushes the top of u into r and then pop from u*/
/*compares top elements, does something if the two not equal, pop 
elements*/

main.cpp

#include <fstream>
#include <string>
#include "stackarray.h"


using namespace std;

int main() 
{ 

   string line; // name of my identifier used to read in data
   string word; // individual characters
   stackType<string> u;
   stackType<string> r;
   stackType<string> s;

   ifstream inData("input.txt"); // declaration of inData
   ofstream outData("output.txt"); // declaration of outData

while (getline( inData, line )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/

while (!isEmptyStack())/*there are still characters*/
{
    u.push(word);
    s.push(word);     /*pushes characters of word into stacks u and s*/
}

while (!u.isEmptyStack())/*u is not empty*/
{
    u.top(r);      /*pushes the top of u into r and then pop from u*/
    u.pop();
}
while ( !s.isEmptyStack() )/*s is not empty*/
{

   if ( u.top() == r.top() )      /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
      outData<<word<<" is a Palindrome"<<endl;
   }

   else
   {
      u.pop();
      r.pop();
      outData<<word<<"is NOT a Palindrome"<<endl; /*based on above, print appropriate output*/ 
   }

}
}
    inData.close(); // closes inData
    outData.close(); // closes outData

    system("pause");
    return 0;
}

.h file

#ifndef H_StackType
#define H_StackType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>

class stackType
{
      
      public:
             const stackType<Type>& operator=(const stackType<Type>&);
             void initializeStack();
             bool isEmptyStack();
             bool isFullStack();
             void destroyStack();
             void push(const Type& newItem);
             Type top();
             void pop();
             stackType(int stackSize = 100);
             stackType(const stackType<Type>& otherStack);
             ~stackType();
             
             
      private:
              int maxStackSize;
              int stackTop;
              Type *list;
              void copyStack(const stackType<Type>& otherStack);
};

template<class Type>
void stackType<Type>::copyStack(const stackType<Type>& otherStack)
{
     delete [] list;
     maxStackSize = otherStack.maxStackSize;
     stackTop = otherStack.stackTop;
     
     list = new Type[maxStackSize];
     assert(list != NULL);
     
     for(int j = 0; j < stackTop; j++)
     list[j] = otherStack.list[j];
     
}

template<class Type>
stackType<Type>::stackType(const stackType<Type>& otherStack)
{
      list = NULL;
      
      copyStack(otherStack);
      
}

template<class Type>
const stackType<Type>& stackType<Type>::operator=(const stackType<Type>& otherStack)
{
      if (this != &otherStack)
      copyStack(otherStack);
      
      return *this;
}

template<class Type>
void stackType<Type>::initializeStack()
{
     stackTop = 0;
}

template<class Type>
void stackType<Type>::destroyStack()
{
     stackTop = 0;
}

template<class Type>
bool stackType<Type>::isEmptyStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
bool stackType<Type>::isFullStack()
{
     return(stackTop == maxStackSize);
}

template<class Type>
void stackType<Type>::push(const Type& newItem)
{
     if(!isFullStack())
     {
         list[stackTop] = newItem;
         
         stackTop++;
     }
     
     else
         cerr<<"Cannot add to a full stack."<<endl;
}

template<class Type>
Type stackType<Type>::top()
{
     assert(stackTop != 0);
     
     return list[stackTop - 1];
}

template<class Type>
void stackType<Type>::pop()
{
     if(!isEmptyStack())
     {
        stackTop--;
     }
     else
        cerr<<"Cannot remove from an empty stack."<<endl;
}

template<class Type>
stackType<Type>::stackType(int stackSize)
{
     if(stackSize <= 0)
     {
       cerr<<"The size of the array to hold the stack must "
           <<"be positive."<<endl;
       cerr<<"Creating an array of size 100."<<endl;
   
       maxStackSize = 100;
     }
       
      else
        maxStackSize = stackSize;
        
        stackTop = 0;
        
        list = new Type[maxStackSize];
}

template<class Type>
stackType<Type>::~stackType()
{
     delete [] list;
}


#endif

I'd still get rid of that header file and just use the stl stack. Did you check out the link I posted about stacks? It would help you a lot with this. http://www.cplusplus.com/reference/stl/stack/

I'm just starting at the top of your code and noting what I see wrong so hopefully it will make sense.

For this, I'm going to assume that the input is "a b b a" just so I don't have to type so much.

After the line while(getline( inData, line)) line contains the whole string "a b b a". Now you need to somehow break that up into the individual letters. I suggested stringstream earlier and still stand by that suggestion. Check the second post for my example.

The line while(!isEmptyStack()) is going to produce an error because it is a function of stackType. Thus a stackType must be in front of it. Plus, that's not what you'd want in that spot. As mentioned above, I placed the string stream output in that field and had it store each character into word.

The next while loop just clears the contents of u, it doesn't do anything with r. To put things into a stack, you must push them onto the stack. What you want to do is push the top of u onto r.

while ( !s.isEmptyStack() )/*s is not empty*/
{

   if ( u.top() == r.top() )      /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
      outData<<word<<" is a Palindrome"<<endl;
   }

   else
   {
      u.pop();
      r.pop();
      outData<<word<<"is NOT a Palindrome"<<endl; /*based on above, print appropriate output*/ 
   }

}

In this piece of code, you compare 1 character, and then produce output, compare the next and produce output. You want to produce the output after you have checked every letter, hence why my pseudo code had the output outside of the last while loop.

You also need a way to determine if it is close to a palindrome or not, which is not accounted for in what you have posted.


That's all I see right now. See if that helps.

hey what post was sstream? i can't find it.

It's actually my first post on this thread. Right after your initial question I believe.

I received an output stating I "Cannot add to a full stack" after doing

while (!u.isEmptyStack())/*u is not empty*/
{
    u.push(word);
               /*pushes the top of u into r and then pop from u*/
    u.pop();
}

Also, is using sstream mandatory? I am also not understand how I can receive the outputs outside of the while loop. Can you please explain.


main.cpp

#include <fstream>
#include <string>
#include <sstream>
#include "stackarray.h"


using namespace std;

int main() 
{ 

   stringstream ss;
   string line; // name of my identifier used to read in data
   string word; // individual characters
   stackType<string> u;
   stackType<string> r;
   stackType<string> s;

   ifstream inData("input.txt"); // declaration of inData
   ofstream outData("output.txt"); // declaration of outData

while (getline( inData, line )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/

ss << word;

}
while ( ss << word)/*there are still characters*/
{
    u.push(word);
    s.push(word);     /*pushes characters of word into stacks u and s*/
}

while (!u.isEmptyStack())/*u is not empty*/
{
    u.push(word);
               /*pushes the top of u into r and then pop from u*/
    u.pop();
}
while ( !s.isEmptyStack() )/*s is not empty*/
{

   if ( u.top() == r.top() )    /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
   }

   else
   {
      u.pop();
      r.pop();
   }

}
outData<<word<<" is a Palindrome"<<endl;

    inData.close(); // closes inData
    outData.close(); // closes outData

    system("pause");
    return 0;
}

No, using stringstream is not mandatory, but manipulating a string isn't all that easy and since it could be different, stringstream is the easiest way to do it.

while (getline( inData, line )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/

ss << word;
}

line contains the string, you would want to put that into ss instead of word. This while loop should also enclose all the lines of code between it and the output, otherwise it will only check one line from the file and then end.

while ( ss << word)/*there are still characters*/

You want to put what's in ss into word, so reverse the sign. >>

while (!u.isEmptyStack())/*u is not empty*/
{
    u.push(word);
               /*pushes the top of u into r and then pop from u*/
    u.pop();
}

You want to push the top of u into r, so you should see r.push(/*something*/); in this while, not u.push().

while ( !s.isEmptyStack() )/*s is not empty*/
{

   if ( u.top() == r.top() )    /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
   }

   else
   {
      u.pop();
      r.pop();
   }

}

This while does nothing except pop the values out of both stacks. I would use if(!(u.top() == r.top())) and remove the else. If the two values are the same, that's great, but that doesn't really help us. What we need to know is if they aren't the same, and keep track of that.

outData<<word<<" is a Palindrome"<<endl;

That just automatically assumes all the words entered are palindromes despite what the above lines of code say. You need a case if they are equal, if they are close to being equal and if they aren't equal.

Am I on the right track with my case statement? For some reason, I don't think I am doing it right.

#include <fstream>
#include <string>
#include <sstream>
#include "stackarray.h"


using namespace std;

int main() 
{ 

   stringstream ss;
   string line; // name of my identifier used to read in data
   string word; // individual characters
   stackType<string> u;
   stackType<string> r;
   stackType<string> s;

   ifstream inData("input.txt"); // declaration of inData
   ofstream outData("output.txt"); // declaration of outData

while (getline( inData, line )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/

ss << line;

}
while ( ss >> word)/*there are still characters*/
{
    u.push(word);
    s.push(word);     /*pushes characters of word into stacks u and s*/
}

while (!u.isEmptyStack())/*u is not empty*/
{
    r.push(u.top());
               /*pushes the top of u into r and then pop from u*/
    u.pop();
}
while ( !s.isEmptyStack() )/*s is not empty*/
{

  if(!(u.top() == r.top()))    /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
   }


}
  switch (!(u.top() == r.top()))
  {
   case 'u.top() == r.top():
   return outData<<word<<" is a Palindrome"<<endl;
   case 'u.top() != r.top()':
   return outData<<word<<" is NOT a Palindrome"<<endl;
   case 'u.top != r.top() ':
   return outData<<word<<" is ALMOST a Palindrome"<<endl;
   }

    inData.close(); // closes inData
    outData.close(); // closes outData

    system("pause");
    return 0;
    
}

On the plus side, except for a few things, the beginning looks right.

while ( !s.isEmptyStack() )/*s is not empty*/
{

  if(!(u.top() == r.top()))    /*compares top elements, does something if the two not equal, pop elements*/
   {
      u.pop();
      r.pop();
   }


}

First, you want to pop from u and r even if the two letters are equal, so you'd want the pop's outside of the if statement. The way I figured to handle this was to get a counter that determined how many differences there were between the two words and then divide by two.

The dividing is because if one letter is off, it will be counted twice. Example.
a b c e b a
a b e c b a
s s d d s s
Technically they are only one letter off (remove either the c or the e and it would be a palindrome), so dividing by two says that it's one letter. I figure two or more letters off isn't close enough.

switch (!(u.top() == r.top()))
  {
   case 'u.top() == r.top():
   return outData<<word<<" is a Palindrome"<<endl;
   case 'u.top() != r.top()':
   return outData<<word<<" is NOT a Palindrome"<<endl;
   case 'u.top != r.top() ':
   return outData<<word<<" is ALMOST a Palindrome"<<endl;
   }

Switch only takes in a bit, so you need to replace !(u.top() == r.top()) with something else. I'd suggest the counter variable I suggested earlier. The cases would be 0 meaning no differences between the words, 1 meaning that it's one letter off, and default (to catch the rest of the numbers) meaning that 2 or more letters are off.

The output screen came up and then disappear again! This is what I have after your last reply.

#include <fstream>
#include <string>
#include <sstream>
#include "stackarray.h"


using namespace std;

int main() 
{ 

   stringstream ss;
   string line; // name of my identifier used to read in data
   string word; // individual characters
   stackType<string> u;
   stackType<string> r;
   stackType<string> s;
   int counter = 0;

   ifstream inData("input.txt"); // declaration of inData
   ofstream outData("output.txt"); // declaration of outData

while (getline( inData, line )) // reads words in file using getline to capture all spaces
{ /*something to break up line into individual character*/

ss << line;

}
while ( ss >> word)/*there are still characters*/
{
    u.push(word);
    s.push(word);     /*pushes characters of word into stacks u and s*/
}

while (!u.isEmptyStack())/*u is not empty*/
{
    r.push(u.top());
               /*pushes the top of u into r and then pop from u*/
    u.pop();
}
while ( !s.isEmptyStack() )/*s is not empty*/
{
   int counter = 0;

  if(!(u.top() == r.top()))    /*compares top elements, does something if the two not equal, pop elements*/
   {
      counter++;
      counter/2;
   }
      u.pop();
      r.pop();

}
  switch (counter)
  {
   case '0':
   outData<<word<<" is a Palindrome"<<endl;
   case '1':
   outData<<word<<" is NOT a Palindrome"<<endl;
   case '2':
   outData<<word<<" is ALMOST a Palindrome"<<endl;
   }
    inData.close(); // closes inData
    outData.close(); // closes outData

    system("pause");
    return 0;
    
}
while ( !s.isEmptyStack() )/*s is not empty*/
{
   int counter = 0;

  if(!(u.top() == r.top()))    /*compares top elements, does something if the two not equal, pop elements*/
   {
      counter++;
      counter/2;
   }
      u.pop();
      r.pop();

}

  switch (counter)
  {
   case '0':
   outData<<word<<" is a Palindrome"<<endl;
   case '1':
   outData<<word<<" is NOT a Palindrome"<<endl;
   case '2':
   outData<<word<<" is ALMOST a Palindrome"<<endl;
   }

You don't want to initialize the counter inside the while loop cause it will reset to zero when it checks the next letter.
a b c a
a c b a
It check a - a, do nothing to counter, the pop, reset counter to zero check b - c, increment counter, pop, the reset counter to zero.

You also want to divide the result of counter after the while loop finishes, otherwise you are going to get odd results.

In the switch, since counter is an integer, you can just write case 0: . And you don't want case 2, you want default cause if there are 4 letters different, your switch will not output anything where as default will catch any numbers that aren't 0 or 1.

Also, the switch now states that if one letter is off, it's not a palindrome, but if 2 letters are off, it's almost a palindrome. Re-read what I posted previous to fix that. Along that line, you need to write breaks into the switch so that if case 0 is right, it only writes 'is a palindrome' to the file. With what you have, you will get this as the output.

a b b a is a Palindrome
a b b a is ALMOST a Palindrome
a b b a is NOT a Palindrome.

You do also realize that the output is being writting to the file, meaning only one line will print to the screen. You won't be able to see the output on the screen unless you include iostream and use cout. while (getline( inData, line )) should enclose the three whiles and the switch. With what you have now, it goes through all the lines, and only determines if the last line of the input file is a palindrome, almost is, or isn't.

You need to add ss.clear() in your code otherwise you will never be able to compare more data. To put more data into stringstream, the old data must be erased and that's what clear does. I'd suggest either before ss << line; or after your switch.

I would suggest you try indenting your code so you can see what lies within a loop and what doesn't. Look back at the pseudo code I provided (2nd post of the thread I believe). You can see that it's easier to see that the three whiles are within the first one. It can also help when trying to check the correctness of a code.

commented: Good posts on this thread! +8
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.