nate12457 0 Newbie Poster

Can anyone tell me why this is happening? All the code and input files are below.


Stack.h

#include <iostream>
#include <vector>
using namespace std;

const char FILE_NAME[] = "palindrome.txt";

typedef char StackElement;

class Stack {
	private:
		vector<StackElement> myVec;
	public:
		bool Empty()
		{ return (myVec.size() == 0); }
		void Push(StackElement item);
		StackElement Top();
		void Pop();
		void ResetStack();
		friend ostream& operator<<(ostream& out, const Stack& s);
};

bool isPalindrome(ifstream& in, Stack a);

Stack.cpp

#include <iostream>
#include <string>
#include <cassert>
#include <cctype>
#include <sstream>
#include <fstream>
#include "Stack.h"
using namespace std;

/*       Purpose: To add an item to the stack.
    Precondition: There is more memory available to give the vector and parameter 1 is a character.
   Postcondition: Parameter 1 is appended to private member myVec. */
void Stack::Push(StackElement item)
{	assert(myVec.max_size() > myVec.size());
	myVec.push_back(item);
}

/* Precondition: The stack is not empty.
        Purpose: Returns the top element of the stack. */
StackElement Stack::Top()
{	assert(!Empty());
	return (myVec[myVec.size() - 1]);
}

/*      Purpose: The last element is removed from the stack.
   Precondition: The stack is not empty. */ 
void Stack::Pop()
{	assert(!Empty());
	myVec.pop_back();
}
// Purpose: All elements are removed from the stack.
void Stack::ResetStack()
{	while (myVec.size() > 0)
		myVec.pop_back();
}


// Purpose: Overloaded output operator
ostream& operator<<(ostream& out, const Stack& s)
{	for (int i = 0; i < s.myVec.size(); i++)
		out << s.myVec[i];
	out << endl;
	return out;
}

/* Purpose: Returns true if the line read from the file is a palindrome. 
   The line will be cout before the return.  */
bool isPalindrome(ifstream& in, Stack a)
{	string backward, forward;
	char fromFile;
	Stack b;

	in.get(fromFile);
	while(fromFile != '\n')
	{	b.Push(fromFile);
		if (isalpha(fromFile))
			a.Push(char(tolower(fromFile)));
		in.get(fromFile);
	}
	while (!a.Empty())
	{	backward += a.Top();
		a.Pop();
	}
	for (int i = backward.length() - 1; i > -1; i--)
			forward += backward[i];
	cout << b;
	return (backward == forward);
}

main.cpp

#include <iostream>
#include <fstream>
#include <cassert>
#include "Stack.h"
using namespace std;
int main() {
	
	ifstream inFile;
	Stack stack1;

	inFile.open(FILE_NAME);
	assert(inFile.is_open());
	cout.setf(ios::boolalpha);
	cout << "Each line from the file is displayed with true or false below it for the palindrome check.\n\n\n";
	while (!inFile.eof())
		cout << isPalindrome(inFile, stack1) << endl << endl;
	inFile.close();
}

Contents of input file palindrome.txt (it ends with a newline, even though it doesn't look like it on here)

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.