Palindrome incorrect output

What I am trying to accomplish here is determining whether a word is a palindrome using queues. My program compiles and executes fine. The only problem is that I am getting the wrong output and need some assistance. I put a bool variable isPalin, but I do not know if I am using it correctly.


Current output:

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


Expected output:

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



main.cpp


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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*/
}

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;
}




queue.h


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
#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 
Last edited on
First question..why aren't you using std::queue</*...*/> ??
what is that?
This is what I have now...however, it is still giving me the wrong output


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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#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


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*/
}

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*/
      {
      isPalin = false;
      break;  
      }
  else
  {
      u.deleteQueue();
      r.deleteQueue();
  }

}

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

ss.clear();

}
    inData.close(); // closes inData
    outData.close(); // closes outData
    
    system("PAUSE");
    return 0;
}
Last edited on
Topic archived. No new replies allowed.