5 Replies - 429 Views - Last Post: 13 October 2012 - 05:20 AM Rate Topic: -----

#1 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Block of code ignore, relating to a pure a virtual function.

Posted 12 October 2012 - 04:59 PM

Unfortunately lines 24 through 34 fails to be called. The cout statement at 39 fails, reads 0.
During menu selection prompt, I select case 2, to convert all contents to upper case.


Contents of text file:

Quote

1 The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
5 The quick brown fox jumps over the lazy dog
1 The quick brown fox jumps over the lazy dog
2 The quick brown fox jumps over the lazy dog
3 The quick brown fox jumps over the lazy dog
4 The quick brown fox jumps over the lazy dog
10th The quick brown fox jumps over the lazy dog



main program
#include <iostream>
#include <fstream>
#include <string>
#include <vector>
using namespace std;

class FileFilter
{
//private:
	//since no input is being accepted directly from the user during runtime.
public:
	virtual char transform(char ch) = 0;
	
	void doFilter(fstream &in, fstream &out)
	{
		vector<string> lineVector;
		int iVec = 0; //for error diagnosing

		//while(!in.eof())
		//{
			
		string line1;

		while(getline(in, line1))
		{
			for(int i = 0; i < line1.length(); i++)
			{
				line1[i] = transform(line1[i]);	// dynamic binding
				cout << "-> " << line1[i] << endl;	// doesn't display
			}

			lineVector.push_back(line1);
			iVec++;
		}

		for (int i = 0; i < lineVector.size(); i++)
			out << line1;

		cout << "\n size of iVec: " << iVec << endl;
	}
};

//////////////////////////////////////////////////////
class ToUpper	:	public FileFilter
{

public:
	char transform (char ch)
	{
		return toupper(ch);
	}
};

//////////////////////////////////////////////////////
class Encryption : public FileFilter
{
private:
	int keyNum;

public:
	char transform(char ch)
	{
		return ch + keyNum;
	}

	//constructor
	Encryption (int encKey) 
	{
		keyNum = encKey;
	}

};

//////////////////////////////////////////////////////
class Unchanged	: public FileFilter
{
	char transform(char ch) 
	{
		return ch;
	}
};


//////////////////////////////////////////////////////
void menu();
string getInFname();
void currentContents(fstream&);

int main()
{

	fstream inFile, outFile;
	fstream toUpper, encrypt, copy;

	string inF, outF;	//fileName of files
	string line;

	int offset;
	int choice;

	inF = getInFname();
	inFile.open(inF, ios::in);

	if (inFile.fail())
	{
		cout << "\nFile couldn't open... shutting down.\n";
		system("pause");
		return 0;
	}

	cout << "\nCurrent file's contents :\n";
	currentContents(inFile);

	cout << endl;

	//output file
	//outFile.open("daOut.txt", ios::out);
			
	ToUpper tuObj1; //Must be declared outside switch block.

	do 
	{
		menu();
		cout << "Your selection: ";
		cin >> choice;

		switch(choice)
		{
		case 1:
			int enc;
			cout << "Enter an encryption number: ";
			cin >> enc;

			//Encryption eObj1(enc);

			break;
		case 2:			
			// to all upper case
			//inFile.open(inF, ios::in); no difference
			toUpper.open("upper.txt", ios::out);

			tuObj1.doFilter(inFile, toUpper);

			cout << "\nUpper operation completed...\n";
			toUpper.close();

			break;

		case 3:

			break;
		}
	} while (choice != 4);
	//while (!outFile)
	//
	//	cout << "File opening error, please re-enter name: ";
	//	cin >> outFileName;
	//}


	//ToUpper file1;
	//file1.doFilter(inFile, outFile);
	inFile.close();
	//outFile.close();

	system("pause");
	return 0;
}

string getInFname()
{
	string inF;
	cout << "Enter the input filename: ";
	cin >> inF;		//da
	return inF += ".txt";
}


void menu()
{
	cout << "\nSelection one of the following options.";
	cout << "\n1 to perform encryption.";
	cout << "\n2 transform to all uppercase.";
	cout << "\n3 to display unchanged copy of the file.";
	cout << "\n4 to quit program.";
}

void currentContents(fstream& file)
{
	string line;
	//while (!file.eof())
	//{
		while(getline(file,line))
		{
			cout << line << endl;	
		}
	//}
}


Is This A Good Question/Topic? 0
  • +

Replies To: Block of code ignore, relating to a pure a virtual function.

#2 jjl  Icon User is offline

  • Engineer
  • member icon

Reputation: 1072
  • View blog
  • Posts: 4,532
  • Joined: 09-June 09

Re: Block of code ignore, relating to a pure a virtual function.

Posted 12 October 2012 - 06:17 PM

You should check to see if your file exists and opened successfully using
is_open

http://www.cplusplus...stream/is_open/
Was This Post Helpful? 0
  • +
  • -

#3 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Block of code ignore, relating to a pure a virtual function.

Posted 12 October 2012 - 06:58 PM

Yup the file is_open..
1st statement, in line 13, did display.


public:
	virtual char transform(char ch) = 0;
	
	void doFilter(fstream &in, fstream &out)
	{
		vector<string> lineVector;
		int iVec = 0; //for error diagnosing

		//while(!in.eof())
		//{
		
		if (in.is_open())
			cout << "\nYes, it's open\n";
		else
			cout << "\nNot open\n";
		string line1;
		while(getline(in, line1))
		{
			for(int i = 0; i < line1.length(); i++)
			{
				line1[i] = transform(line1[i]);	// dynamic binding
				cout << "-> " << line1[i] << endl;	// doesn't display
			}

			lineVector.push_back(line1);
			iVec++;
		}

		for (int i = 0; i < lineVector.size(); i++)
			out << line1;

		cout << "\n size of iVec: " << iVec << endl;
	}
};

Was This Post Helpful? 0
  • +
  • -

#4 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Block of code ignore, relating to a pure a virtual function.

Posted 12 October 2012 - 08:42 PM

Part of your problem is that when you display the file contents with your currentContents() function you read the file until you get an error. Then later you try to re-read the file in your doFilter() function. But the only thing is that the file steam is in an error state because you have never reset the stream error flags, plus the file pointer will be pointing to the location of the error (eof). You need to clear the stream error state and reset the file pointer to the beginning of the file.

Edit: This is why you should really check the state of all the stream error flags, not individual flags.


Jim

This post has been edited by jimblumberg: 12 October 2012 - 08:43 PM

Was This Post Helpful? 1
  • +
  • -

#5 mgrex  Icon User is offline

  • D.I.C Head

Reputation: 3
  • View blog
  • Posts: 182
  • Joined: 25-March 10

Re: Block of code ignore, relating to a pure a virtual function.

Posted 12 October 2012 - 10:00 PM

Thanks for the suggestion. I just implemented .clear() flag, and unfortunately the problem persists.

The if statement in line 22, failed to execute as a result of resetting the error state.

class FileFilter
{
//private:
	//since no input is being accepted directly from the user during runtime.
public:
	virtual char transform(char ch) = 0;
	
	void doFilter(fstream &in, fstream &out)
	{
		vector<string> lineVector;
		int iVec = 0; //for error diagnosing

		//while(!in.eof())
		//{
		
		/*if (in.is_open())
			cout << "\nYes, it's open\n";
		else
			cout << "\nNot open\n";*/
		if (in.fail())
			in.clear(); //cout << "\nfail haha\n"; //was displayed
		if (in.fail())
			cout << "\nhaha fail\n";	//not displayed
		string line1;
		while(getline(in, line1))
		{
			for(int i = 0; i < line1.length(); i++)
			{
				line1[i] = transform(line1[i]);	// dynamic binding
				cout << "-> " << line1[i] << endl;	// doesn't display
			}

			lineVector.push_back(line1);
			iVec++;
		}

		for (int i = 0; i < lineVector.size(); i++)
			out << line1;

		cout << "\n size of iVec: " << iVec << endl;
	}
};

Was This Post Helpful? 0
  • +
  • -

#6 jimblumberg  Icon User is offline

  • member icon


Reputation: 4018
  • View blog
  • Posts: 12,401
  • Joined: 25-December 09

Re: Block of code ignore, relating to a pure a virtual function.

Posted 13 October 2012 - 05:20 AM

First you should be checking for all error conditions, something like:
if(!in)

The fail() function only checks the failbit or badbit flags, but does not check the eofbit.

Secondly you didn't reset the file pointer to the beginning of the file so the first time you try to read something you will be in an error state again.

Jim
Was This Post Helpful? 1
  • +
  • -

Page 1 of 1