Palindrome Check With Stack & Input/Output Files

I'm having trouble with my palindrome check. I am designing a stack class that is implemented with a vector, so creating the stack functions, use vector functions. But it still not calculating the lines right, I'm getting the error message for every line of the input file:

The palindrome check line by line until the end of the file. It should ignore case, punctuation, and spacing, copying lines that are palindromes to output file and printing the error message in place of those that are not.

Input:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
Go hang a salami. I'm a lasagna hog. 
Cigar? Toss it in a can, it is so tragic. 
Dog as a devil deified lived as a god. 
(Thanks to Jason Hanson) 
Stressed? No tips ? Spit on desserts. 
(Thanks to Cbmac62) 
No, Sir! Away! A papaya war is on! 
(Thanks to Kathy) 
O Memsahib, Bart! Rabbi has memo! 
(Thanks to Nancy Lincoln and The Simpsons) 
Egad! A base tone denotes a bad age. 
(Thanks to David Zimet) 
I roamed under it, a tired, nude Maori. 
Yawn. Madonna Fan? No damn way! 
Sums are not set as a test on Erasmus. 
No, Mel Gibson is a casino's big lemon.


This should be the output file but it isn't
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
gohangasalamiimalasagnahog
cigartossitinacanitissotragic
dogasadevildeifiedlivedasagod
***Line is not a palindrome***
stressednotipsspitondesserts
***Line is not a palindrome***
nosirawayapapayawarison
***Line is not a palindrome***
omemsahibbartrabbihasmemo
***Line is not a palindrome***
egadabasetonedenotesabadage
***Line is not a palindrome***
iroamedunderitatirednudemaori
yawnmadonnafannodamnway
sumsarenotsetasatestonerasmus
nomelgibsonisacasinosbiglemon


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
#include <iostream>
#include <vector>
using namespace std;

template <class T>
class Stack{

private:
	vector<T> data;

public:
	bool Empty() 
	/* --- Add a value to the stack ---
	*
	* Receive: The Stack containing this function (implicitly)
	*          A value to be added to a Stack
	* Return:  The Stack (implicitly), with value added at its
	*          top, provided there's space
	*************************************************************/
	{
		return data.empty();
	}
	
	void Push(T item) //adds item to stack
	/* --- Display values stored in the stack ---
	*
	* Receive: The Stack containing this function (implicitly)
	*          The ostream out
	* Output:  The Stack's contents, from top down, to out
	*************************************************************/
	{
		data.push_back(item);
	}	
	
	T Top() //returns item at top of stack
	/* --- Return value at top of the stack ---
	*
	* Receive: The Stack containing this function (implicitly)
	* Return:  The value at the top of the Stack, if nonempty;
	*          else a "garbage value"
	* Output:  "Stack empty" message if stack is empty
	*************************************************************/
	{
	if(data.back()>-1)
	return data.back();

	else
	{
		cerr<<"Stack is empty\n";
		T garbage = 0;
		return garbage;
	}
	}
	
	void Pop() 
	/* --- Remove value at top of the stack ---
	*
	* Receive: The Stack containing this function (implicitly)
	* Return:  The Stack containing this function (implicitly)
	*           with its top value (if any) removed
	* Output:  "Stack-empty" message if stack is empty.
	*************************************************************/
	{
	data.pop_back();
	}

	friend ostream& operator<<(ostream&out, const Stack &s) //overloaded output operator
	{
	while(!data.empty())
	{
	out<<data.back()<<endl;
	data.pop_back();
	}return out;
	}
};


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
#include <iostream>
#include <vector>
#include <string>
#include <fstream>
#include "Stack.h"

using namespace std;

void CheckPalindrome()
{
	ifstream inFile;
	ofstream outFile;
	inFile.open("palindrome.txt");
	outFile.open("outfile.txt");

	if (inFile.fail()||outFile.fail())
	{
		cout<<"File opening failed"<<endl;
		exit(1);
	}


	Stack<char> s;		
	string holdPalindrome="";
	string comparePalindrome="";
	char next;
	string line;

	while(!inFile.eof())
	{

		getline(inFile,line);
		
		int size = line.size();

		for(int i = 0; i<size;i++)
		{
			next = line[i];
			if (isalnum(next))
			{
				towlower(next);
				s.Push(next);
				holdPalindrome += next;
			}
			}	

		while(!s.Empty())
		{
			comparePalindrome += s.Top();
			s.Pop();
		}

		if (comparePalindrome==holdPalindrome)
		{
			outFile<<holdPalindrome;
			holdPalindrome="";
			comparePalindrome="";
		}

		else
		{
			outFile<<"***Line is not a palindrome***";
			holdPalindrome="";
			comparePalindrome="";
		}
			
		outFile<<endl;

	}

	inFile.close();
	outFile.close();
}

int main()
{

	CheckPalindrome();

	return 0;
}
whats the error?
Topic archived. No new replies allowed.