Code Review
Code Review
#include <fstream>
#include <string>
#include <map>
using namespace std;
class algorithm {
fstream fileIn;
string fileName;
string newFileName;
string message;
bool restart;
char chars;
int key;
public:
void encrypt();
void decrypt();
};
void algorithm::encrypt(){
restart = false;
cout <<"-Encryption Chosen-" <<endl;
cout <<"Enter name of the file you want to encrypt:";
do {
restart = false;
getline(cin, fileName);
fileIn.open(fileName);
if(!fileIn){
cout <<"File could not be found" <<endl;
cout <<"Re-enter file name:";
restart = true;
}
}while(restart);
while(fileIn.get(chars)){
message += chars;
}
if(message[i] >=127){
message[i] -=94;
}
else if(message[i] ==19){
message[i] = '\n';
}
//write to file
newFileName = "encrypted-file.txt";
ofstream encryptedFile(newFileName);
encryptedFile <<message;
encryptedFile.close();
cout <<"-ENCRYPTION SUCCESSFUL-"<<endl;
cout <<"Look for " << newFileName<< " in the directory" <<endl;
}
void algorithm::decrypt(){
cout <<"-Decryption Chosen-" <<endl;
cout <<"Enter the name of the file you want to decrypt:";
do {
restart = false;
getline(cin, fileName);
fileIn.open(fileName);
if(!fileIn){
cout <<"File could not be found" <<endl;
cout <<"Re-enter file name:";
restart = true;
}
}while(restart);
while(fileIn.get(chars)){
message += chars;
}
if(message[i] <32){
message[i] +=94;
}
}
//write to file
newFileName = "decrypted-file.txt";
ofstream decryptedFile(newFileName);
decryptedFile <<message;
decryptedFile.close();
cout <<"-DECRYPTION SUCCESSFUL-"<<endl;
cout <<"Look for " << newFileName<< " in the directory" <<endl;
}
int main()
{
algorithm alg;
string choice;
bool reset =false;
cout <<"===============Encryption
Algorithm===============" <<endl;
cout <<"Press E to encrypt:\n";
cout <<"Press D to decrypt:" <<endl;
getline(cin, choice);
do{
reset = false;
switch (choice[0])
{
case 'E':
alg.encrypt();
break;
case 'e':
alg.encrypt();
break;
case 'D':
alg.decrypt();
break;
case 'd':
alg.decrypt();
break;
default:
cout <<"Invalid input press E or D:" <<endl;
getline(cin, choice);
reset = true;
break;
}
}while(reset);
return 0;
}