Hello

I am trying to determine whether a word is a palindrome or not using queues in my program below. However, I am currently having a problem. When I compile and execute my program, the output screen comes up and disappears very fast. Afterwards, I ran the program using the command prompt.

The error that I received was

"Assertion failed: !isEmptyQueue(), file C:\Documents and Settings\winuser\Deskto
p\/queue.h, line 60

This application has requested the Runtime to terminate it in an unusual way.
Please contact the application's support team for more information.
"


I went to look around in my queue.h to find the problem, but I could not find the problem at all. I even rebuilt my queue.h from scratch again using the exact information that is in a book. If anyone can help me on this. I appreciate it. Thanks


main.cpp

#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "queue.h"

using namespace std;

int main() 
{ 

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

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


   u.initializeQueue();
   r.initializeQueue();
   s.initializeQueue();

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.addQueue(word);
    s.addQueue(word);     /*adds characters of word into queues u and s*/
}

while (!u.isEmptyQueue()) /*u is not empty*/
{
    r.addQueue(u.front());
               /*adds to the front of u into r and then delete from u*/
    u.deleteQueue();
}


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

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

    counter/=2;

  switch (counter)
   {
   case 0:
   outData<<word<<" is a Palindrome"<<endl;
   break;
   
   case 1:
   outData<<word<<" is NOT a Palindrome"<<endl;
   break;
   }
   ss.clear();
}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}

queue.h

#ifndef H_queueType
#define H_queueType

#include <iostream>
#include <cassert>

using namespace std;

template<class Type>
class queueType // public queueADT<Type>
{
      public:
             const queueType<Type>& operator=(const queueType<Type>&);
             bool isEmptyQueue() const;
             bool isFullQueue() const;
             void initializeQueue();
             Type front() const;
             Type back() const;
             void addQueue(const Type& queueElement);
             void deleteQueue();
             queueType(int queueSize = 100);
             queueType(const queueType<Type>& otherQueue);
             ~queueType();
             void printQueue();
             bool operator== (const queueType<Type>&);
             bool operator!= (const queueType<Type>&);
             
      private:
              int maxQueueSize;
              int count;
              int queueFront;
              int queueRear;
              Type *list;
              bool isEqual(const queueType<Type>&);
};

template<class Type>
bool queueType<Type>::isEmptyQueue() const
{
     return (count == 0);
}

template<class Type>
bool queueType<Type>::isFullQueue() const
{
     return (count == maxQueueSize);
}

template<class Type>
void queueType<Type>::initializeQueue()
{
     queueFront = 0;
     queueRear = maxQueueSize - 1;
     count = 0;
}

template<class Type>
Type queueType<Type>::front() const
{
     assert(!isEmptyQueue());
     return list[queueFront];
}

template<class Type>
Type queueType<Type>::back() const
{
     assert(!isEmptyQueue());
     return list[queueRear];
}

template<class Type>
void queueType<Type>::addQueue(const Type& newElement)
{
     if (!isFullQueue())
     {
       queueRear = (queueRear + 1) % maxQueueSize;
       
       count++;
       list[queueRear] = newElement;
     }
     
     else
       cout<< "Cannot add to a full queue."<<endl;
}

template<class Type>
void queueType<Type>::deleteQueue()
{
     if (!isEmptyQueue())
     {
       count--;
       queueFront = (queueFront + 1) % maxQueueSize;
     }
     
     else
     cout <<"Cannot remove from an empty queue"<<endl;
     
}

template<class Type>
queueType<Type>::queueType(int queueSize)
{
  if (queueSize <= 0)
  {
    cout<<"Size of the array to hold the queue must "
        <<"be positive."<<endl;
    cout<<"Creating an array of size 100."<<endl;
    
    maxQueueSize = 100;
  }
  
  else
  
     maxQueueSize = queueSize;
     
     queueFront = 0;
     queueRear = maxQueueSize - 1;
     count = 0;
     list = new Type[maxQueueSize];
}

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


template<class Type>
bool queueType<Type>::isEqual(const queueType<Type>& otherQueue) 
{
bool bRet = false;

if (otherQueue.maxQueueSize == maxQueueSize && otherQueue.queueFront == queueFront)
{
bRet = true;
for (int j = 0; j < queueFront; ++j) 
{
if (otherQueue.list[j] != list[j]) 
{
// cout << "!=( " << j << ") " << otherStack.list[j] << "!=" << list[j];
bRet = false;
break;
}
}
}
return bRet;
}

template<class Type>
bool queueType<Type>::operator==(const queueType<Type>& otherQueue) 
{
return isEqual(otherQueue);
}

template<class Type>
bool queueType<Type>::operator!=(const queueType<Type>& otherQueue) 
{
return !isEqual(otherQueue);
}


template<class Type>
void queueType<Type>::printQueue()
{
     
for (int x = queueFront-1; x >= 0; --x)    
{        
cout << list[x] << '\n';    
}  
     
     
}



#endif

That's the purpose of the assert, to halt the program if a particular condition is not met. So you're calling your front() or back() methods on what apparently is an empty queue.

Thank you. I got my output to come up however, I have another problem.

My output is saying

abbaccaccabba is a Palindrome
12332123321 is a Palindrome
123212421 is a Palindrome

when it is suppose to be

abbaccaccabba is a Palindrome
12332123321 is a Palindrome
123212421 is NOT a Palindrome


Do you see anything that is wrong in my code? I appreciate it.

#include <fstream>
#include <string>
#include <sstream>
#include <stdlib.h>
#include "queue.h"

using namespace std;

int main() 
{ 

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

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


   u.initializeQueue();
   r.initializeQueue();
   s.initializeQueue();

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.addQueue(word);
    s.addQueue(word);     /*adds characters of word into queues u and s*/
}

while (!u.isEmptyQueue()) /*u is not empty*/
{
    r.addQueue(u.front());
    u.deleteQueue();
               /*adds to the front of u into r and then delete from u*/
}


while (!s.isEmptyQueue())/*s is not empty*/
{
      s.deleteQueue();
      u.addQueue(word);
  
  if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
   {
     counter++;
   }

      u.deleteQueue();
      r.deleteQueue();
}

    counter/=2;

  switch (counter)
   {
   case 0:
   outData<<word<<" is a Palindrome"<<endl;
   break;
   
   case 1:
   outData<<word<<" is NOT a Palindrome"<<endl;
   break;
   }
   ss.clear();
}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}

If counter is one, counter/2 = 0 (since it is an int), so it's giving you a false reading. Maybe you want to break out of the while once you sense any difference or something.

Edited 7 Years Ago by jonsca: n/a

I tried to figure out what you meant by breaking out of the while, but I can't. Can you explain further please? Whatever I do, I just can't get it to count right!

Edited 7 Years Ago by NinjaLink: n/a

I feel that my problem with my counter is around here. This is what I've been working on:

while (!s.isEmptyQueue())/*s is not empty*/
{
      s.deleteQueue();
      u.addQueue(word);
  
  if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
   {
     counter++;
   }
      u.deleteQueue();
      r.deleteQueue();
}

counter/2;


  switch (counter)
   {
   case 0:
   outData<<word<<" is a Palindrome"<<endl;
   break;
   
   default:
   outData<<word<<" is NOT a Palindrome"<<endl;
   break;
   }
   ss.clear();
}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}
if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
   {
        u.deleteQueue();
        r.deleteQueue();
        break;
   }

Since you don't care as long as the pairs of digits/letters are off by at least one, just break out of the while loop.

Do you see with your other setup that you would never get to case 1?

so I shouldn't use counter in the if statement at all? or should I place the counter in the while loop after the if statement? I think the placement of the counter is killing me! lol

while (!s.isEmptyQueue())/*s is not empty*/
{
      s.deleteQueue();
      u.addQueue(word);
  
  if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
      {
      u.deleteQueue();
      r.deleteQueue();
      break;
      }
      counter++;
}
counter/2;


  switch (counter)
   {
   case 0:
   outData<<word<<" is a Palindrome"<<endl;
   break;
   
   default:
   outData<<word<<" is NOT a Palindrome"<<endl;
   break;
   }
   ss.clear();
}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}

Edited 7 Years Ago by NinjaLink: n/a

Why do you need a counter? You could make a boolean variable and use that in an if statement instead of your switch.

bool isPalin = true;
while (!s.isEmptyQueue())/*s is not empty*/
{
      s.deleteQueue();
      u.addQueue(word);
  
  if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
      {
      u.deleteQueue();
      r.deleteQueue();

      isPalin = false;
      break;
      }
      
}

if(isPalin)
cout<<"is a palindrome"<<endl;
else
cout<<"is not a palindrome"<<endl;

Edited 7 Years Ago by jonsca: n/a

This is what I have so far with your help. I was never good at using boolean values, but I want to give it a shot. I got the first word correct, but after that, mom should of been a Palindrome! Did I do the boolean value incorrectly? Thanks for helping so far!

current output:

racecar is a Palindrome
123456789 is NOT a Palindrome
mom is NOT a Palindrome
dog is NOT a Palindrome

bool isPalin=true;

while (!s.isEmptyQueue())/*s is not empty*/
{
      s.deleteQueue();
      u.addQueue(word);
  
  if(!(u.front() == r.front()))    /*compares front elements, does something if the two not equal, delete elements*/
      {
      u.deleteQueue();
      r.deleteQueue();
      isPalin=false;
      break;
      }
}

if (isPalin)
{
outData<<word<<" is a Palindrome"<<endl;
}
else
outData<<word<<" is NOT a Palindrome"<<endl;

ss.clear();

}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}

What happens when you put in noon?

I've kind of overlooked it until now but shouldn't your queue have a function to advance the queue and get rid of the elements in the front? Seems like you make a fresh queue for each pair of letters or numbers. My feeling is you should split the string in half (tossing out the middle character if there are an odd number), feed them into separate queues (one set flipped around) and then go through the both one by one? (so compare element 1 of queue A and element 1 of queue B, "next()" your queue, compare element 2 of A and 2 of B, etc.)

Edited 7 Years Ago by jonsca: n/a

I see I see....sorry, nevermind about the second part... I thought deleteQueue was clearing the whole thing.

Edited 7 Years Ago by jonsca: n/a

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