I started a new thread since my last thread was getting to long. I am using stacks to find out if a string of characters is a palindrome, is not a palindrome, and is almost a palindrome. However, my output comes up and then disappears when i compile. So far, I have yet to find out what is the problem. Any advice?

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 <sstream>
#include <stack>
#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;
    
}

header 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

line 49 of the *.cpp file -- that should probably be /= operator.

When I run your program I get this error

Assertion failed: stackTop != 0, file c:\dvlp\try1\try1\stackarray.h, line 109
Press any key to continue . . .

I updated my code with your last post, but the output still disappears. Do you think I have an error writing my header file?

I also tried #include <stack> and the output box stays up but it shows blank and then eventually gives a windows error to close.

Any idea guys? I really want this program to work, but the output box comes up and then disappears when I use stackarray.h, when i use #include <stack>, the output window stays up for small period and then I get hit with a windows error to close. What do you guys think?

Run it from the command line. If you're running from an IDE, depending on the IDE and the configuration, the console window will disappear when the program aborts due to a fatal run-time error. You need to see the output and the exact error, so run it from the command line and see what happens. Then start sticking debugging statements in there, see exactly how far it gets, so you can zoom in on the exact offending line.

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