Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

my code error is \'the program hung while executing option 3 to encrypt with the

ID: 3539437 • Letter: M

Question

my code error is 'the program hung while executing option 3 to encrypt with the source and destination files are specified.'

and my code is

#include <iostream>
#include <fstream>
#include <string>

using namespace std;

int main()
{
string source;
    string destination;
int menu;
const int sour=1,
dest=2,
encrypt=3,
decrypt=4,
exit=0;

do
    {
cout<<"File Encryption and Decryption Filter"<<endl
<<"1) Enter source filename"<<endl
<<"2) Enter destination filename"<<endl
<<"3) Encrypt file"<<endl
<<"4) Decrypt file"<<endl
<<"0) Exit"<<endl
<<"select: ";
cin>>menu;
cout<<endl;

switch(menu)
{
case sour:
{
cout<<"1: Enter source filename"<<endl;
cin>>source;
}
break;

case dest:
{
cout<<"2: Enter destination filename"<<endl;
cin>>destination;
}
break;

case encrypt:
    {
if((source.empty())||(destination.empty()))
{
cout<<"You havenot entered source file"<<endl;
cout<<endl;
}
else
{
ifstream file1;
file1.open(source.c_str());
ofstream file2;
file2.open(destination.c_str());
char c;
while(!file1.eof())
{
file1>>c;
char newc= int(c)+12;
file2<<newc<<" ";
}

                file1.close();
file2.close();
}
}
break;

case decrypt:
{
if((destination.empty())||(source.empty()))
{
cout<<"You have not entered destination file"<<endl;
cout<<endl;
}
else
{
ofstream file1;
file1.open(source.c_str());
ifstream file2;
file2.open(destination.c_str());
char c;
while(!file2.eof())
{
file2>>c;
char newc=int(c)-12;
file1<<newc;
}
file1.close();
file2.close();
}
}
break;
}
}while (menu != exit);
system("pause");
return 0;   
}

problem is

File encryption is the science of writing the contents of a text file in a secret code. Your encryption/decryption program should work like a filter, reading the contents of one file, modifying the data into a code, and then writing the coded contents out to a second file. The second file will be a version of the first file, but written in a secret code.

Although there are complex encryption techniques, you should come up with a simple one of your own. For example, you could read the first file one character at a time, and add 10 to the ASCII code of each character before it is written to the second file.

The program should have a menu that allows the user to perform the following tasks:


The source and destination file names should be displayed as part of the menu, if the user has specified.

For example, at the program start the menu could look like:

1: Enter source filename
2: Enter destination filename
3: Encrypt file
4: Decrypt file
0: Exit


After both file names are entered, the menu could look like:

1: Enter source filename
2: Enter destination filename
3: Encrypt file (source.txt)
4: Decrypt file (encrypted.txt)
0: Exit

Explanation / Answer

1. You have not created the file which would be the source file and the destination file. create it before hand and enter the text to be encrpted inside the file.


2. Also, to stop the program from unsucessfull termination include a check whether the file is created or not before encrypting or decrypting


3. You can also create the file if the file is not already created when asking for the source and destination file


#include <iostream>

#include <fstream>

#include <string>


using namespace std;


int main()

{

string source;

string destination;

int menu;

const int sour=1,

dest=2,

encrypt=3,

decrypt=4,

exit=0;


do

{

cout<<"File Encryption and Decryption Filter"<<endl

<<"1) Enter source filename"<<endl

<<"2) Enter destination filename"<<endl

<<"3) Encrypt file"<<endl

<<"4) Decrypt file"<<endl

<<"0) Exit"<<endl

<<"select: ";

cin>>menu;

cout<<endl;


switch(menu)

{

case sour:

{

cout<<"1: Enter source filename"<<endl;

cin>>source;

ifstream file1;

file1.open(source.c_str(),ios::app);

file1.close();

}

break;


case dest:

{

cout<<"2: Enter destination filename"<<endl;

cin>>destination;

ofstream file2;

file2.open(destination.c_str(),ios::app);

file2.close();

}

break;


case encrypt:

{

if(source.empty())

{

cout<<"You havenot entered source file"<<endl;

cout<<endl;

}

else

{

ifstream file1;

file1.open(source.c_str(),ios::app);

ofstream file2;

file2.open(destination.c_str(),ios::app);

char c;

while(!file1.eof())

{

file1>>c;

char newc= int(c)+12;

file2<<newc<<" ";

}


file1.close();

file2.close();

}

}

break;


case decrypt:

{

if((destination.empty())||(source.empty()))

{

cout<<"You have not entered destination file"<<endl;

cout<<endl;

}

else

{

ofstream file1;

file1.open(source.c_str(),ios::app);

ifstream file2;

file2.open(destination.c_str(),ios::app);

char c;

while(!file2.eof())

{

file2>>c;

char newc=int(c)-12;

file1<<newc;

}

file1.close();

file2.close();

}

}

break;

}

}while (menu != exit);

system("pause");

return 0;   

}