0

I find myself oddly perplexed on this homework assignment. The idea is to create a Palindrome program, using a specific header the professor wants us to use, but for some reason, when I run it, right after I enter the phrase the program crashes on me.

Here is the program

#include   <iostream>
#include   <ctime>
#include   "STACK.h"
using namespace std;

int main(void)
{
// Declare variables
time_t          a;
STACK<char, 80>     s;
STACK<char, 80>     LR;
STACK<char, 80>     RL;
char            c;
char            c1;
char            c2;

// Displays the current time and date
time(&a);
cout << "Today is " << ctime(&a) << endl;

// Prompts the user to enter the string
cout << "Enter a phrase: ";
while(cin.get(c) && (c != '\n'))
{

    if(isalpha(c)) 
    {
        c = toupper(c); 
        LR.PushStack(c);
        s.PushStack(c);
    }

}

// Copies s into RL in reverse order
while(!(s.EmptyStack() ) )
{
    c = s.PopStack();
    RL.PushStack(c);
}

// Checks for Palindrome
while(!(LR.EmptyStack() ) )
{
    c1 = LR.PopStack();
    c2 = RL.PopStack();

    if( c1 != c2) 
    {
    break;
    }
}


// Displays the result
if(LR.EmptyStack() )
{
    cout << "Palindrome";
}
else
{
    cout << "Not a Palindrome";
}

return 0;
}

And here is the header (I am not allowed to change this)

#ifndef STACK_H
#define STACK_H
template <class T, int n>
class STACK
{ private: T a[n];
    int counter;
  public:
     void  MakeStack() { counter = 0; }
 bool  FullStack()
    { return (counter == n) ? true : false ; }
 bool EmptyStack()
     { return (counter == 0) ? true : false ; }
 void  PushStack(T x)
     { a[counter] = x; counter++; }
 T PopStack()
     { counter--; return a[counter]; }
};
    #endif
user1675108
  • 83
  • 1
  • 2
  • 12

1 Answers1

2

You are not calling MakeStack, which will set STACK initial size (0).

KCH
  • 2,794
  • 2
  • 23
  • 22
  • 3
    That's what constructors are for, so don't consider it your fault - it's library design that created this problem:) – KCH Sep 23 '12 at 23:41