Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C++ (http://www.go4expert.com/forums/cpp/)
-   -   Palindrome that will read a file (http://www.go4expert.com/forums/palindrome-read-file-t27969/)

darknite121 7Mar2012 05:26

Palindrome that will read a file
 
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. :)

dearvivekkumar 7Mar2012 11:11

Re: Palindrome that will read a file
 
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;
}


darknite121 8Mar2012 02:51

Re: Palindrome that will read a file
 
Do I have to remove the getline() and replace it?

xpi0t0s 8Mar2012 06:39

Re: Palindrome that will read a file
 
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)?

darknite121 8Mar2012 20:49

Re: Palindrome that will read a file
 
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?

darknite121 8Mar2012 21:09

Re: Palindrome that will read a file
 
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.

xpi0t0s 9Mar2012 00:54

Re: Palindrome that will read a file
 
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".


All times are GMT +5.5. The time now is 19:52.