Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

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

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Computer Aided Database Design

Authors: Antonio Albano, Valeria De Antonellis, A. Di Leva

1st Edition

0444877355, 978-0444877352

More Books

Students also viewed these Databases questions