Please help me fix my program, do NOT write a new one.
I believe almost everything is working except that I can not get
the copy and encryption files to execute correctly.
If I switch the order of the code in "Part 3: Process data",
which ever code block goes first, executes correctly and the rest
dont..
I can not figure out why, I would appreciate if you could help
me fix my code.
Here is the promp to know what I am doing. The program is
written in C++.
_____________________________________________________________________________________________
Chapter on Polymorphism and Virtual Functions: Prompt File
Filter
A file filter reads an input file, transforms it in some way,
and writes the results to an output file. Write an abstract file
filter class that defines a pure virtual function for transforming
a character. Create one subclass of your file filter class that
performs encryption, another that transforms a file to all
uppercase, and another that creates an unchanged copy of the
original file.
The encryption class should have a constructor that takes an
integer as an argument and uses it as the encrytion key.
_________________________________________________________________________________________________
#include <iostream>
//Header required for user inputs/outputs
#include <fstream>
//Header required to use fstream file in/out opeartions
#include <cctype>
//Header required for special c string functions
using namespace std;
//***** Class Declarations *****
//Define abstract class
class Filter
{
public:
void doFilter(fstream& in, fstream& out);
//Function open,process, and close files
protected:
virtual char transform(char ch) = 0;
//Virtual function declared
};
//*****Subclasses from abstract class*****
class UpperCaseFilter : public Filter //Upper
case filter class
{
protected:
char transform(char ch)
{
return toupper(ch);
}
};
class EncryptionFilter : public Filter
{
int key;
public:
EncryptionFilter(int a)
{
key = a;
}
char transform(char c)
{
return (c + key);
}
};
class CopyFilter : public Filter
{
protected:
char transform(char c)
{
return c;
}
};
//*****Function declarations ******
//Function declaration for processing files
void Filter::doFilter(fstream& in, fstream& out)
{
char ch = in.get();
while (ch != EOF)
{
out.put(transform(ch));
ch = in.get();
}
}
//Function declaration for printing files
void printFile(fstream& file)
{
file.clear();
//Clear buffer
file.seekg(0, ios::beg); //Move to the
start of the file
//Print file
char ch = file.get(); //Gets
file name
while (ch != EOF)
//Continue with each character until end of file
{
cout << ch;
ch = file.get();
}
file.clear();
//Clear buffer
file.seekg(0, ios::beg); //Move to the
start of the file
}
int main()
{
//Part 1: Declare variables
const int LENGTH = 81;
//Defines max length of
file name
char inputFileName[LENGTH],
outFileNameU[LENGTH],
outFileNameE[LENGTH],
outFileNameC[LENGTH];
//Declare two file name variables
fstream inputFile, outputFileU, outputFileE,
outputFileC; //Declare two fstream objects
UpperCaseFilter upperFilterFile;
//Declare class object for upper case filter
int key = 2;
//Encryption key
EncryptionFilter encryptedFile(key);
//Declare class object for encrypted file
CopyFilter copyFile;
//Part 2: Get file name from user and open file
for input
cout << "Enter name of input file: ";
cin >> inputFileName;
inputFile.open(inputFileName, ios::in);
if (!inputFile)
{
cout << inputFileName << "
could not be opened.\n";
return(0);
}
cout << "Enter name of output uppercase
file: ";
cin >> outFileNameU;
outputFileU.open(outFileNameU, ios::out);
if (!outputFileU)
{
cout << outFileNameU << "
could not be opened." << endl;
return(0);
}
cout << "Enter name of output copied file:
";
cin >> outFileNameC;
outputFileC.open(outFileNameC, ios::out);
if (!outputFileC)
{
cout << outFileNameC << "
could not be opened." << endl;
return(0);
}
cout << "Enter name of output encryption
file: ";
cin >> outFileNameE;
outputFileE.open(outFileNameE, ios::out);
if (!outputFileE)
{
cout << outFileNameE << "
could not be opened." << endl;
return(0);
}
//Part 3: Process data
upperFilterFile.doFilter(inputFile, outputFileU);
//Call function
outputFileU.close();
//Close file to save
copyFile.doFilter(inputFile, outputFileC);
//Call function
outputFileC.close();
//Close file to save
encryptedFile.doFilter(inputFile, outputFileE);
//Call function
outputFileE.close();
//Close file to save
//Part 4: Display outputs
cout << "\n*****Display original file.*****"
<< endl;
printFile(inputFile);
//Print original file
cout << endl;
outputFileU.open(outFileNameU, ios::in);
//Open uppercase
file
cout << "\n*****Uppercase file is.*****"
<< endl;
printFile(outputFileU);
//Print uppercase file
cout << endl;
outputFileC.open(outFileNameC, ios::in);
//Open copied file
cout << "\n*****Copied file is.*****" <<
endl;
printFile(outputFileC);
//Print copied file
cout << endl;
outputFileE.open(outFileNameE, ios::in);
//Open encrypted
file
cout << "\n*****Encrypted file is.*****"
<< endl;
printFile(outputFileE);
//Print encrypted file
cout << endl;
// Close both files
inputFile.close();
outputFileU.close();
outputFileE.close();
outputFileC.close();
return 0;
}
void doFilter(ifstream &in, ofstream &out) that is called to perform the actual filtering. The member function for transforming a single character should have the prototype char transform(char ch)
Please help me fix my program, do NOT write a new one. I believe almost everything is working except that I can not get
-
answerhappygod
- Site Admin
- Posts: 899604
- Joined: Mon Aug 02, 2021 8:13 am
Please help me fix my program, do NOT write a new one. I believe almost everything is working except that I can not get
Join a community of subject matter experts. Register for FREE to view solutions, replies, and use search function. Request answer by replying!