I am receiving error messages for this palindrome problem. I am reading in a file and outputting the results if it is a palindrome or not using stacks. Any ideas as to why I am receiving error messages below.

include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________

using namespace std;
//____________________________________________________________________________________

class CommandLineException
{	
	public:
	CommandLineException(int max, int actual)
	{	
		cout << " Too many command line arguments. " << endl;
	}
};
//____________________________________________________________________________________


class FileException
{
	public:
	FileException(char* fn)
	{
		cout << " File " << fn << " could not be opened. " << endl;
	}
};
//____________________________________________________________________________________

class Stack_Empty_Exception
{	
	public:
	Stack_Empty_Exception(char* a)
	{	
		cout << " I am . " << a <<" empty" << endl;
	}
};

//____________________________________________________________________________________

class Stack_Full_Exception
{
	public:
	Stack_Full_Exception(char* fn)
	{
		cout << " I am " << fn << " full. " << endl;
	}
};
//____________________________________________________________________________________

class Stack 
{
	public:
		Stack(int);
		~Stack();
		bool isFull(void); // check less than size	
		bool isEmpty(void);  
		void Push(char v);
		char Pop(void);
		
		
	private:
		
		int size;
		int top_of_stack;
		char* s;

};
//____________________________________________________________________________________

Stack::Stack()
{
	int sz = 100;
	size = sz;
	top_of_stack = -1;
	s = new char[size];
}
//____________________________________________________________________________________

Stack::~Stack()
{
	if(s)
	{
		delete[]s;
	}
}

//____________________________________________________________________________________

bool Stack::isFull(void)
{
	return top_of_stack >= size - 1;
}

//____________________________________________________________________________________
bool Stack::isEmpty(void)
{
	return top_of_stack < 0;
}
//____________________________________________________________________________________
void Stack::Push( char v)
{
	if (isFull())
		throw Stack_Full_Exception();
			s[++top_of_stack] = v;
}
//____________________________________________________________________________________
char Stack::Pop(void)
{
	if (isEmpty())
		throw Stack_Empty_Exception();

	return s[top_of_stack--];
}
//____________________________________________________________________________________

int main(int argc, char * argv[]) 
{

	try
	{
	char infile[100];   // input file
	char outfile[100];  // output file

	
		if (argc == 1)
		{
			cout << " Enter the input file name. " << endl;
			cin >> infile;
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
		}
		
		
	        else if (argc == 2)
		{
			strcpy(infile, argv[1]);
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
			
		}
		else if ( argc == 3)
		{
			strcpy(infile, argv[1]);
			strcpy(outfile, argv[2]);
		}
		else
		{
			throw CommandLineException(2,argc -1);
		}		
	
	ifstream i(infile);
		if(!i)
			throw FileException(infile);

	ofstream o(outfile);
		if(!o)
			throw FileException(outfile);

        	o.close();
		i.close();
	} catch(...)
		{
		cout <<  " Program Terminated. " << endl;
		exit(EXIT_FAILURE);
		}
	

	void is_Palindrome(isstream& i, ostream& o)
	{
		for(;;)
		{
			string word;
			i >> word;
			if ( i.eof())
				break;
			o << endl;
			o << word << " is a palindrome. " << endl;
		}   // end of for loop
	}  // end of is_Palindrome function.

	bool check_Palindrome( string& word)
	{
		Stack reverse;

			for ( int i = 0; i < word.length(); i++)
			{
				reverse.push(word[i]);
			}
			for ( int j = 0; j < word.length(); j++)
			{
				if (word[j] != reverse.pop())
					
					return false;
				else
					return true;
			}


	}  // end of check_palindrome
	





	return 0;
}

error messages:

mpiling...
p02.cpp
D:\MSDev98\CS2613\p02\p02.cpp(79) : error C2511: 'Stack::Stack' : overloaded member function 'void (void)' not found in 'Stack'
        D:\MSDev98\CS2613\p02\p02.cpp(59) : see declaration of 'Stack'
D:\MSDev98\CS2613\p02\p02.cpp(111) : error C2512: 'Stack_Full_Exception::Stack_Full_Exception' : no appropriate default constructor available
D:\MSDev98\CS2613\p02\p02.cpp(118) : error C2512: 'Stack_Empty_Exception::Stack_Empty_Exception' : no appropriate default constructor available
D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'isstream' : undeclared identifier
D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'i' : undeclared identifier
D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2065: 'o' : undeclared identifier
D:\MSDev98\CS2613\p02\p02.cpp(178) : error C2275: 'ostream' : illegal use of this type as an expression
        d:\vc98\include\iosfwd(257) : see declaration of 'ostream'
D:\MSDev98\CS2613\p02\p02.cpp(179) : error C2448: '<Unknown>' : function-style initializer appears to be a function definition
D:\MSDev98\CS2613\p02\p02.cpp(183) : error C2065: 'word' : undeclared identifier
D:\MSDev98\CS2613\p02\p02.cpp(183) : warning C4552: '>>' : operator has no effect; expected operator with side-effect
D:\MSDev98\CS2613\p02\p02.cpp(184) : error C2228: left of '.eof' must have class/struct/union type
D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2563: mismatch in formal parameter list
D:\MSDev98\CS2613\p02\p02.cpp(186) : error C2568: '<<' : unable to resolve function overload
        could be 'class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &__cdecl std::endl(class std::basic_ostream<unsigned short,struct std::char_traits<unsigned short> > &)'
        d:\vc98\include\ostream(377) : see declaration of 'endl'
        or       'class std::basic_ostream<char,struct std::char_traits<char> > &__cdecl std::endl(class std::basic_ostream<char,struct std::char_traits<char> > &)'
        d:\vc98\include\ostream(372) : see declaration of 'endl'
        or       'class std::basic_ostream<_E,_Tr> &__cdecl std::endl(class std::basic_ostream<_E,_Tr> &)'
        d:\vc98\include\ostream(367) : see declaration of 'endl'
D:\MSDev98\CS2613\p02\p02.cpp(187) : error C2297: '<<' : illegal, right operand has type 'char [19]'
D:\MSDev98\CS2613\p02\p02.cpp(192) : error C2601: 'check_Palindrome' : local function definitions are illegal
Error executing cl.exe.

p02.obj - 14 error(s), 1 warning(s)

Recommended Answers

All 8 Replies

why stacks?
Heres a palindrome proggy i did for another board.

#include <iostream>
#include <string>
#include <cctype>

int IsMirrorPalindrome( const char* word, size_t length )
{
   if( length <= 1) return 1;
   else
   return ( (word[0] == word[length-1]) && IsMirrorPalindrome(word+1,length-2));
}

int IsPalindrome(std::string test)
{
   std::string tempcopy;
   std::string::iterator iter = test.begin();
   const std::string::iterator end = test.end();
   while ( iter != end )
   {
      const char c = *iter;
      if ( (!std::ispunct(c)) && (!std::isspace(c)) ) tempcopy.push_back(std::tolower(c));
      ++iter;
   }

   if (IsMirrorPalindrome(tempcopy.c_str(),tempcopy.length()))
      return 1;
   else
      return 0;
}


int main()
{
   std::string test;
   std::cout<<" enter string.... ";
   std::getline(std::cin,test);
   if (IsPalindrome(test))
      std::cout<<std::endl<<"palindrome"<<std::endl;
   else
      std::cout<<std::endl<<" not palindrome"<<std::endl;
   getchar();
}

ok lets go thru your code....

your exception classes will need default constructors or arguments passed to them.

destructor doesnt need if(s). just the delete[]s will do nicely.

throw Stack_Full_Exception();
should be
throw Stack_Full_Exception;
or
throw Stack_Full_Exception(args);
depending on available constructors. see point 1.


You cant nest other functions inside main!!!!
Thats illegal.
Your funcs to check if a palindrome is never called from main.
Fix those and repost if that dont do it.

I have to use a stack, will make those corrections and let you know what happens.

thanks

Okay, the problem I am having now is that once compiled and I will input the I/O file names. It will state that the file cannot be opened. Why?

//____________________________________________________________________________________________________________________


#include <iostream>
#include <string>
#include <cmath>
#include <iomanip>
#include <fstream>
#include <cmath>
#include <vector>
#include <cstdlib>
//____________________________________________________________________________________

using namespace std;
//____________________________________________________________________________________

class CommandLineException
{	
	public:
	
	CommandLineException(int max, int actual)
	{	
		cout << " Too many command line arguments. " << endl;
	}
};

//____________________________________________________________________________________


class FileException
{
	public:
	
	FileException(char* fn)
	{
		cout << " File " << fn << " could not be opened. " << endl;
	}
};

//____________________________________________________________________________________

class Stack 
{
	public:
		Stack();
		~Stack();
		bool isFull(void); 
		bool isEmpty(void);  
		void Push(char v);
		char Pop(void);
		class Stack_Empty_Exception{public:Stack_Empty_Exception();};
		class Stack_Full_Exception{public:Stack_Full_Exception();};

	private:
		
		int size;
		int top_of_stack;
		char* s;

};
//____________________________________________________________________________________
Stack::Stack_Empty_Exception::Stack_Empty_Exception()
{
	
	cout << " The stack is empty. " << endl;
}
//____________________________________________________________________________________
Stack::Stack_Full_Exception::Stack_Full_Exception()
{
	
	cout << " The stack is full. " << endl;
}
//____________________________________________________________________________________
Stack::Stack()
{
	int sz = 100;
	size = sz;
	top_of_stack = -1;
	s = new char[size];
}
//____________________________________________________________________________________

Stack::~Stack()
{
	if(s)
	
		delete[]s;
	
}

//____________________________________________________________________________________

bool Stack::isFull(void)
{
	return top_of_stack >= size - 1;
}

//____________________________________________________________________________________
bool Stack::isEmpty(void)
{
	return top_of_stack < 0;
}
//____________________________________________________________________________________
void Stack::Push( char v)
{
	if (isFull())
		throw Stack_Full_Exception();
			s[++top_of_stack] = v;
}
//____________________________________________________________________________________
char Stack::Pop(void)
{
	if (isEmpty())
		throw Stack_Empty_Exception();

	return s[top_of_stack--];
}
//____________________________________________________________________________________

bool is_Palindrome(ifstream& i, ofstream& o);
void Palindrome_mgr(string str1, ofstream& o);
//____________________________________________________________________________________

int main(int argc, char* argv[]) 
{

	try
	{
	char infile[255];   // input file
	char outfile[255];  // output file

	
		if (argc == 1)
		{
			cout << " Enter the input file name. " << endl;
			cin >> infile;
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
		}
		
		
	        else if (argc == 2)
		{
			strcpy(infile, argv[1]);
			cout << " Enter the output file name. " << endl;
			cin >> outfile;
			cout << endl;
			
		}
		else if ( argc == 3)
		{
			strcpy(infile, argv[1]);
			strcpy(outfile, argv[2]);
		}
		else
		{
			throw CommandLineException(2,argc -1);
		}		
	
	ifstream i(infile);
		if(!i)
			throw FileException(infile);

	ofstream o(outfile);
		if(!o)
			throw FileException(outfile);

		string word;
		for(;;)
		{
			if(i.eof())
				break;
			
			i >> word;


		}
		Palindrome_mgr(word, o);




        	o.close();
			i.close();
	} catch(...)
		{
		cout <<  " Program Terminated. " << endl;
		exit(EXIT_FAILURE);
		}


	
	




	return 0;
}



bool is_Palindrome(string check1, string check2 )
{
		if (check1 == check2)
			return true;
		else
			return false;
		
}  // end of is_Palindrome function.




	

void Palindrome_mgr(string str1, ofstream& o)
{
			string str2;
			bool swapped;
			Stack s;
			for ( int i = 0; i < str1.length(); i++)
			{
				s.Push(str1[i]);
				str2 += s.Pop();


			}
			swapped = is_Palindrome(str1,str2);
			
			if (swapped)
				cout << str1 << " is a palindrome." << endl;
			else
				cout<< str1 << " is not a palindrome. " << endl;

}	// end of Palindrome_mgr.

You have included cmath twice, but that's not likely to cause the problem you describe. When programs compile, but then don't run appropriately, it is harder to debug them. In this case the syntax of obtaining file names and associating them with files, etc. seem appropriate. Therefore, I would try printing the file names to the screen using cout statements to be sure they have been input appropriately. IF that doesn't work, then posting the exact output that is unexpected and/or error statements you encounter would be helpful. If that still doesn't work I'd start commenting out stuff until I got back to a bare bones program without all the fluff of try/catch etc. to confound the issue and proceed until I got the program running. Then I'd add back sections, one at time and testing each one as they are reintroduced until I tracked the problem down to a (given set of) line(s).

okay i figured it out. the file names were different. However, when it reads in the file it only reads in the last word and checks for palindrome.

string word;  //declare a single string
for(;;)  
{
  if(i.eof())  //if find EOF 
   break;    //stop
			
  i >> word;  ///else read in each word, but don't do anything with it.
}
Palindrome_mgr(word, o); //send last word read in to this function.

Do you see any problem with logic in the comments I wrote?
You will do yourself a big favor by testing code as you write it, one
step at a time, and not as one big bolus.

BTW: You shouldn't be testing for eof() to break out of the loop as
it will likely cause an overread by one. But that would be then next
problem you encounter if you fix the more obvious logic problem.

I got it figured out. Good looking out everyone.

thanks

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.