RSS Forums RSS
Please support our C++ advertiser: Programming Forums

stack palindrome problem?

Join Date: Jun 2005
Location: California
Posts: 92
Reputation: djbsabkcb is an unknown quantity at this point 
Rep Power: 4
Solved Threads: 0
djbsabkcb's Avatar
djbsabkcb djbsabkcb is offline Offline
Junior Poster in Training

Help Re: stack palindrome problem?

  #5  
Sep 14th, 2005
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.
Reply With Quote  
Forums | Blogs | Tutorials | Code Snippets | Whitepapers | RSS Feeds | Advertising
All times are GMT -4. The time now is 5:38 am.
Newsletter Archive - Sitemap - Privacy Statement - Acceptable Use Policy - Contact Us
Forum system based on vBulletin Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
©2003 - 2008 DaniWeb® LLC