Palindrome program

What is messing with me is that when equal is set to false one it is not rest to true again.

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

#include "Queue.h"
#include "Stack.h"

using namespace std;

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
int main()
{
        Queue q;  Stack s;
        string line;


        int length = 0;
        bool equal = true;

        ifstream file;
        file.open("input.txt");

        while(!file.eof())
        {
		
                getline(file,line);
                length =line.length();

                for(int i =0; i<length;i++)
                        line[i] = tolower(line[i]);
                for(int i =0; i<length; i++)
                {
			
                        if(isalpha(line[i]) || isalnum(line[i]))
                        {
                                //cout<<line[i]<<" ";
                                q.enqueue(line[i]);//add item to back of queue
                                s.push(line[i]);//add character to top of stack
                        }
                }
                //cout<<endl<<"***NEW LOOP***"<<endl;
                while(q.empty() !=true)
                {
                        //cout<<q.front()<<"   "<<s.top()<<endl;
                        equal = true;
                        if(q.front() != s.top())
                        {
                                equal = false;
                                break;
                        }
                        q.dequeue();//removes first value from queue
                        s.pop();//removes top value from stack
                }
                if(equal == true)
                        cout<<"This is a palindrome"<<endl;
                else if(equal == false)
                {
                        cout<<"This is not a palindrome"<<endl;
                        equal=true;
                }
        };
        file.close();
        return 0;
}
Last edited on
What is messing with me is that your code is hard to read, because you didn't use code tags:

http://www.cplusplus.com/articles/z13hAqkS/
Alright sorry about that didn't know about that.
What is messing with me is that when equal is set to false one it is not rest to true again.

What makes you believe that is what is happening?
What is happening is when it loops through the file, once it hits a line that would return false it will say the rest are false so(we are only looking at numbers and letters by the way):

Go hang a salami. I'm a lasagna hog. Is a palindrome
Cigar? Toss it in a can, it is so tragic. Is a palindrome
Dog as a devil deified lived as a god. Is a palindrome
(Thanks to Jason Hanson) Is not a palindrome
Stressed? No tips ? Spit on desserts. Is not a palindrome
(Thanks to Cbmac62) Is not a palindrome
No, Sir! Away! A papaya war is on! Is not a palindrome
(Thanks to Kathy) Is not a palindrome

And so on for the rest of the file
That is not because of the value equal takes on. That's because you abort checking through your queue and stack when it's apparent the string is not a palindrome, and don't empty them so that on the next iteration of the loop they still have unprocessed stuff in them from the last iteration.
Alright thanks a lot man.
Topic archived. No new replies allowed.