#include #include #include using namespace std; class Caesar { public: void encrypt(char *inp,char *out,int key); void decrypt(char *inp,char *out,int key); void readText(char *inp); void encrypt1(); void decrypt1(); }; void Caesar::encrypt(char *inp,char *out,int key) { ifstream input; ofstream output; char buf; input.open(inp); output.open(out); buf=input.get(); while(!input.eof()) { if(buf>='a'&&buf<='z') { buf-='a'; buf+=key; buf%=26; buf+='A'; } output.put(buf); buf=input.get(); } input.close(); output.close(); readText(inp); readText(out); } void encrypt1() { char message[100], ch; int i, key; ofstream myfile ("encrypt.dat"); cout << "Enter a message to encrypt: "; cin>>message; cout << "\nEnter key: "; cin >> key; for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch + key; if(ch > 'z'){ ch = ch - 'z' + 'a' - 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch + key; if(ch > 'Z'){ ch = ch - 'Z' + 'A' - 1; } message[i] = ch; } myfile << message[i] << " " ; } } void decrypt1() { char message[100], ch; int i, key; ofstream myfile ("decrypt.txt"); cout << "Enter a message to decrypt: "; cin>>message; cout << "Enter key: "; cin >> key; for(i = 0; message[i] != '\0'; ++i){ ch = message[i]; if(ch >= 'a' && ch <= 'z'){ ch = ch - key; if(ch < 'a'){ ch = ch + 'z' - 'a' + 1; } message[i] = ch; } else if(ch >= 'A' && ch <= 'Z'){ ch = ch - key; if(ch > 'a'){ ch = ch + 'Z' - 'A' + 1; } message[i] = ch; } myfile << message[i] << " " ; } } void Caesar::decrypt(char *inp,char *out,int key) { ifstream input; ofstream output; char buf; input.open(inp); output.open(out); buf=input.get(); while(!input.eof()) { if(buf>='A'&&buf<='Z') { buf-='A'; buf+=26-key; buf%=26; buf+='a'; } output.put(buf); buf=input.get(); } input.close(); output.close(); readText(inp); readText(out); } void Caesar::readText(char *inp) { ifstream input; char buf; input.open(inp); cout<<"\n\n <--- "<\n"; buf=input.get(); while(!input.eof()) { cout<>ch; if(ch==1){ cout<<"\n Enter input file: "; cin>>inp; cout<<"\n Enter output file: "; cin>>out; cout<<"\n Enter key: "; cin>>key; cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n 3. exit.Select choice(1 or 2. Press 0 for exit): "; cin>>choice; do{ switch(choice){ case 1: a.encrypt(inp,out,key); break; case 2: a.decrypt(inp,out,key); break; case 3: exit(0); default: cout<<"\n\n Unknown choice"; } }while(choice!=0); } else if(ch==2) { cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): "; cin>>choice; do{ switch(choice){ case 1: encrypt1(); break; case 2: decrypt1(); break; default: cout<<"\n\n Unknown choice"; } }while(choice!=0); } } Question for the code above what is the theory behind it, describe the programming techniques used in the code and explain how the program reacts to good inputs and bad inputs.
File Stream
#include<iostream>
#include<string>
#include<fstream>
using namespace std;
class Caesar
{
public: void encrypt(char *inp,char *out,int key);
void decrypt(char *inp,char *out,int key);
void readText(char *inp);
void encrypt1();
void decrypt1();
};
void Caesar::encrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='a'&&buf<='z')
{
buf-='a';
buf+=key;
buf%=26;
buf+='A';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void encrypt1()
{
char message[100], ch;
int i, key;
ofstream myfile ("encrypt.dat");
cout << "Enter a message to encrypt: ";
cin>>message;
cout << "\nEnter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch + key;
if(ch > 'z'){
ch = ch - 'z' + 'a' - 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch + key;
if(ch > 'Z'){
ch = ch - 'Z' + 'A' - 1;
}
message[i] = ch;
}
myfile << message[i] << " " ;
}
}
void decrypt1()
{
char message[100], ch;
int i, key;
ofstream myfile ("decrypt.txt");
cout << "Enter a message to decrypt: ";
cin>>message;
cout << "Enter key: ";
cin >> key;
for(i = 0; message[i] != '\0'; ++i){
ch = message[i];
if(ch >= 'a' && ch <= 'z'){
ch = ch - key;
if(ch < 'a'){
ch = ch + 'z' - 'a' + 1;
}
message[i] = ch;
}
else if(ch >= 'A' && ch <= 'Z'){
ch = ch - key;
if(ch > 'a'){
ch = ch + 'Z' - 'A' + 1;
}
message[i] = ch;
}
myfile << message[i] << " " ;
}
}
void Caesar::decrypt(char *inp,char *out,int key)
{
ifstream input;
ofstream output;
char buf;
input.open(inp);
output.open(out);
buf=input.get();
while(!input.eof())
{
if(buf>='A'&&buf<='Z')
{
buf-='A';
buf+=26-key;
buf%=26;
buf+='a';
}
output.put(buf);
buf=input.get();
}
input.close();
output.close();
readText(inp);
readText(out);
}
void Caesar::readText(char *inp)
{
ifstream input;
char buf;
input.open(inp);
cout<<"\n\n <--- "<<inp<<" --->\n";
buf=input.get();
while(!input.eof())
{
cout<<buf;
buf=input.get();
}
input.close();
}
int main()
{
Caesar a;
int choice,key,ch;
char inp[30],out[30];
cout<<"\n\n 1. using file\n 2. Using keyboard input\n\n Select choice(1 or 2): ";
cin>>ch;
if(ch==1){
cout<<"\n Enter input file: ";
cin>>inp;
cout<<"\n Enter output file: ";
cin>>out;
cout<<"\n Enter key: ";
cin>>key;
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n 3. exit.Select choice(1 or 2. Press 0 for exit): ";
cin>>choice;
do{
switch(choice){
case 1:
a.encrypt(inp,out,key);
break;
case 2:
a.decrypt(inp,out,key);
break;
case 3:
exit(0);
default: cout<<"\n\n Unknown choice";
}
}while(choice!=0);
}
else if(ch==2)
{
cout<<"\n\n 1. Encrypt\n 2. Decrypt\n\n Select choice(1 or 2): ";
cin>>choice;
do{
switch(choice){
case 1:
encrypt1();
break;
case 2:
decrypt1();
break;
default: cout<<"\n\n Unknown choice";
}
}while(choice!=0);
}
}
Question
for the code above what is the theory behind it, describe the programming techniques used in the code and explain how the program reacts to good inputs and bad inputs.
![](/static/compass_v2/shared-icons/check-mark.png)
Step by step
Solved in 2 steps
![Blurred answer](/static/compass_v2/solution-images/blurred-answer.jpg)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Computer Networking: A Top-Down Approach (7th Edi…](https://www.bartleby.com/isbn_cover_images/9780133594140/9780133594140_smallCoverImage.gif)
![Computer Organization and Design MIPS Edition, Fi…](https://www.bartleby.com/isbn_cover_images/9780124077263/9780124077263_smallCoverImage.gif)
![Network+ Guide to Networks (MindTap Course List)](https://www.bartleby.com/isbn_cover_images/9781337569330/9781337569330_smallCoverImage.gif)
![Concepts of Database Management](https://www.bartleby.com/isbn_cover_images/9781337093422/9781337093422_smallCoverImage.gif)
![Prelude to Programming](https://www.bartleby.com/isbn_cover_images/9780133750423/9780133750423_smallCoverImage.jpg)
![Sc Business Data Communications and Networking, T…](https://www.bartleby.com/isbn_cover_images/9781119368830/9781119368830_smallCoverImage.gif)