Palindrome that will read a file

darknite121's Avatar, Join Date: Mar 2012
Newbie Member
Hello
I need help on my palindrome code. My palindrome uses stacks and queues. The input must be read from a file. However, my code will run but there is something wrong with it. The program does read the .txt file but it will not get the input from the .txt file and I still need to type an input. Here's my code:

Code:
#include <iostream>
#include <stack>
#include <queue>
#include <string>
#include <fstream>

using namespace std;

void is_palindrome (string);

bool operator==(const stack<char>&, const queue<char>&);
bool operator!=(const stack<char>&, const queue<char>&);


int main(){
    string inp;
	int i;

	ifstream in("input.txt");
        if (!in){
            cout << "Cannot Display Text." << endl;
        }

        while(getline(in, inp)){
        stack<char> *s;
        queue<char> *q;
        s = new stack<char>;
        q = new queue<char>;

        cout << "Input"<<inp;
        getline(cin,inp);

        string::iterator i;
        for (i = inp.begin(); i != inp.end(); i++) {
            s->push(*i);
            q->push(*i);
        }
        cout << inp << " is ";
        if (*s != *q) {
            cout << "not ";
        }
        cout << "a palindrome." << endl;

        delete s;
        delete q;
    }

    return 0;

}







bool operator==(const stack<char> &s1, const queue<char> &q1) {
    bool eq = true;
    stack<char> s = s1;
    queue<char> q = q1;

    if (s.size() == q.size()) {
        while ((s.empty() == false) && (eq == true)) {
            eq = (s.top() == q.front());
            s.pop();
            q.pop();
        }
    } else {
        eq = false;
    }
    return eq;
}

bool operator!=(const stack<char> &s, const queue<char> &q) {
    return !(s == q);
}
Comments and suggestions are welcomed.
0
dearvivekkumar's Avatar, Join Date: Feb 2012
Go4Expert Member
I don't know why you have used getline() under while test condition.

Code:
int main()
{
	string inp;
	//int i;

	ifstream in("input.txt");
	if (!in)
	{
		cout << "Cannot Display Text." << endl;
	}
	
	
	while(!in.eof())
	{
		//cout << "Input"<<inp;
		//getline(cin,inp);
		stack<char> *s = new stack<char>;
		queue<char> *q = new queue<char>;
		inp.clear();
		getline(in, inp);
		string::iterator i;
		for (i = inp.begin(); i != inp.end(); i++) 
		{
			s->push(*i);
			q->push(*i);
		}

		cout << inp << " is ";
		if (*s != *q) {
			cout << "not ";
		}
		cout << "a palindrome." << endl;
		delete s;
		delete q;
	}
	
    return 0;
}
0
darknite121's Avatar, Join Date: Mar 2012
Newbie Member
Do I have to remove the getline() and replace it?
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
No, just think about where you want getline to get a line from. In this code:
Code:
getline(cin,inp);
from your original post, where do you think that input will come from: in (your file) or cin (the console)?
0
darknite121's Avatar, Join Date: Mar 2012
Newbie Member
Now, I changed the getline(cin, inp) to getline(in, inp).

Code:
while(getline(in, inp)){
        stack<char> *s;
        queue<char> *q;
        s = new stack<char>;
        q = new queue<char>;

        cout << inp;
        getline(in, inp);

        string::iterator i;
        for (i = inp.begin(); i != inp.end(); i++) {
            s->push(*i);
            q->push(*i);
        }
        cout<<inp;
        cout << " is ";
        if (*s != *q) {
            cout << "not ";
        }
        cout << "a palindrome." << endl;

        delete s;
        delete q;
    }

    return 0;
}
But there is a problem on the output.
When I remove the first "cout<<inp", only 5 of the input from file will be read, and all of them are "not palindromes".

Example:

some men interpret nine memos is not a palindrome
a man a plan a canal - Panama is not a palindrome
CSC121 is not a palindrome
CSc121 is not a palindrome
women understand men is not a palindrome


When the second "cout<<inp" is removed, only the 4 last inputs are read, and still "not palindromes".

Example:

madam i'm adam is not a palindrome
do geese see god? is not a palindrome
never odd or even is not a palindrome
murder for a jar of red rum is not a palindrome

Any help on how to output the phrases from file all at once and how to ignore the spaces and punctuation marks?
0
darknite121's Avatar, Join Date: Mar 2012
Newbie Member
Okay... So I know now how to output the phrases all at once. This is what I did:

remove the "getline(in, inp)" and the 2nd "cout<<inp;"

Code:
cout << inp;
//getline(in, inp);

string::iterator i;
for (i = inp.begin(); i != inp.end(); i++) {
    s->push(*i);
    q->push(*i);
}
//cout<<inp;
But still, I need help on how to make my program to ignore the punctuation marks and upper case letters.
0
xpi0t0s's Avatar, Join Date: Aug 2004
Mentor
Just strip them out. Copy the letters to a new string, so the old string is "madam i'm adam" but the new string is "madamimadam".