Question: I'm doing a C++ assignment on encryption and decryption. Whenever I debug it says input.txt cannot be opened What have I done wrong? #include #include

I'm doing a C++ assignment on encryption and decryption. Whenever I debug it says "input.txt cannot be opened" What have I done wrong?

#include

#include

using namespace std;

//class

class Encryption

{

protected:

ifstream inFile;

ofstream outFile;

int key;

public:

Encryption(char *inFileName, char *outFileName, int keyValue);

~Encryption();

void setKey(int value);

//virtual function

virtual char transform(char ch) = 0;

//Do work

void encrypt();

void closeFiles();

};

//Open files and set encryption key

Encryption::Encryption(char *inFileName, char *outFileName, int keyValue)

{

inFile.open("input.txt");

outFile.open("output.txt");

setKey(keyValue);

if (!inFile)

{

cout << "The file " << inFileName << " cannot be opened.";

exit(1);

}

if (!outFile)

{

cout << "The file " << outFileName << " cannot be opened.";

exit(1);

}

}

//Closing file

Encryption::~Encryption()

{

inFile.close();

outFile.close();

}

//Method for the encryption key

void Encryption::setKey(int value)

{

key = value;

}

//encryption using virtual member function to transform individual char

void Encryption::encrypt()

{

char ch;

char transCh;

inFile.get(ch);

while (!inFile.fail())

{

transCh = transform(ch);

outFile.put(transCh);

inFile.get(ch);

}

}

//close the files

void Encryption::closeFiles()

{

inFile.close();

outFile.close();

}

//subclass overrides virtual transofmr function

class SimpleEncryption :public Encryption

{

public:

char transform(char ch)

{

return ch + key;

}

SimpleEncryption(char *inFileName, char *outFileName, int key):Encryption(inFileName, outFileName, key)

{}

};

//main function

int main()

{

char inFileName[80], outFileName[80];

int key;

cout << "Enter name of file to encrypt: ";

cin >> inFileName;

cout << "Enter name of the file to receive the encrypted text: ";

cin >> outFileName;

cout << "Enter the encryption key: ";

cin >> key;

SimpleEncryption obfuscate(inFileName, outFileName, key);

//encrypt file

obfuscate.encrypt();

//close

obfuscate.closeFiles();

//decrypt

cout << "Enter the name of the file to receive the decrypted text: ";

cin >> inFileName;

SimpleEncryption obfusdate(outFileName, inFileName, -key);

obfusdate.encrypt();

return 0;

}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!