1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Palindrome that will read a file

Discussion in 'C++' started by darknite121, Mar 6, 2012.

  1. darknite121

    darknite121 New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Philippines
    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. :)
     
  2. dearvivekkumar

    dearvivekkumar New Member

    Joined:
    Feb 21, 2012
    Messages:
    29
    Likes Received:
    5
    Trophy Points:
    0
    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;
    }
    
     
  3. darknite121

    darknite121 New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Philippines
    Do I have to remove the getline() and replace it?
     
  4. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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)?
     
  5. darknite121

    darknite121 New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Philippines
    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?
     
  6. darknite121

    darknite121 New Member

    Joined:
    Mar 6, 2012
    Messages:
    4
    Likes Received:
    0
    Trophy Points:
    0
    Occupation:
    Student
    Location:
    Philippines
    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.
     
  7. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,012
    Likes Received:
    203
    Trophy Points:
    0
    Occupation:
    Senior Support Engineer
    Location:
    England
    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".
     

Share This Page