/****************************************************************** * Programmer
ID: 3642008 • Letter: #
Question
/******************************************************************* Programmer: Roland
*
* Date: April 12, 2012
*
*
* File name: myMain.cpp
*
* Description: Demonstrates basic cin, cout, selection structures, IO manipulation
* and formatting, user-defined functions, array processing, cstring,
* string, file input and output.
*
* Assumptions: Assumes that the user enters an encryption key of upto 128 characters
* that are all lower case. It also assumes the user enters a file name.
*
* Input: keyboard - 128 lower case characters, file name.
*
* Output: screen - Decrypted file.
*
*
********************************************************************/
#include <iostream> //header files
#include <iomanip>
#include <string>
#include <cstring>
#include <fstream>
using namespace std;
string ende[]={"encrypt","decrypt"},buffer,key,newbuffer="";
char filename[80],enmap[128],demap[128],again;
int i,maplength;
char ch;
bool stop = false;
void creatfile ();
void displayMenu (); //function prototypes
void getMenuSelection (char ch);
void getFileName ();
void getKey ();
void EncryptionMap (string,char[]);
void DencryptionMap (string,char[]);
void Encryption (const string,const char[],int,string&);
int main ()
{
creatfile (); //creates a file to encrypt
extern bool stop;
while (!stop) //main program continuous loop that will display menu
{
displayMenu();
cin >> ch;
getMenuSelection (ch);
}
return 0;
}
void creatfile () //function creates file
{
ofstream file;
file.open ("myfile.txt");
file << "This is my file line 1 ";
file << "I have written line two ";
file << "my file line three ";
file << "how many lines do I have to write? ";
file << "This is line five now. ";
file << "My fingers are tired! ";
file << "S E V E N ";
file << "I ate line eight. ";
file << "Almost there. ";
file << "This is my favorite line, number 10!";
file.close();
cout << "file has been written!";
}
void displayMenu () //displays menu options
{
cout << " Do you want to perform a file Encryption or Decryption? Choose an option:" << endl
<< "E for Encryption" << endl
<< "D for Decryption" << endl
<< "Q to quit." << endl;
}
void getMenuSelection (char ch) //switch statement nested in void function
{
extern bool stop;
switch (ch)
{
case 'e':
case 'E':
getFileName ();
getKey ();
EncryptionMap (key, enmap);
Encryption (buffer,demap,maplength,newbuffer);
break;
case 'd':
case 'D':
getFileName ();
getKey ();
DencryptionMap (key,demap);
Encryption (buffer,demap,maplength,newbuffer);
break;
case 'q':
case 'Q':
stop = true;
break;
default:
cout << "Invalid selection. Try again." << endl;
stop = false;
}
}
void getFileName ()
{
ofstream out;
ifstream in;
cout<<"Enter name of your file to manipulate : ";
cin>>filename;
in.open(filename);
if(in.fail())
{
cout<<"input file did not open please check it ";
system("pause");
}
else
cout<<"Enter name of your output file: ";
cin>>filename;
out.open(filename);
}
void getKey () //void function that sends encryption/decryption key characters
{
cout<<"Insert your encryption key (max 128 characters): ";
cin>>key;
if(key.length()>128)
{
cout<<"key too long ";
cout<<"Enter your encryption key (max 128 characters): ";
cin>>key;
}
maplength=key.length();
}
void EncryptionMap (string key,char map[]) // Encrypts the file
{
int i;
for(i=0;i<key.length();i++)
map[i]=key[i]-'a';
}
void DecryptionMap (string key,char map[]) // decrypts then encrypted file
{
int i;
for(i=0;i<key.length();i++)
map[i]=26-(key[i]-'a');
}
void Encryption (const string buffer,const char map[],int len,string& newbuffer)
{
int i=0;
char t,code;
for(i=0;i<buffer.length();i++)
{
cout<<buffer[i]<<" "<<i<<endl;
if(isalpha(buffer[i]))
{if(islower(buffer[i]))
code='a';
else
code='A';
t=buffer[i]-code;
cout<<code<<" "<<buffer[i]<<" "<<t<<" "<<i<<endl;
t=t+map[i%len];
t=t%26;
t=t+code;
newbuffer.push_back(t);
}
else
newbuffer.push_back(buffer[i]);
}
newbuffer.push_back(' ');
}
Explanation / Answer
#include "stdafx.h" #include //header files #include #include #include #include using namespace std; string ende[]={"encrypt","decrypt"},buffer,key,newbuffer=""; char filename[80],enmap[128],demap[128],again; int i,maplength; char ch; bool stop = false; void creatfile (); void getFileName (); void getKey (); void EncryptionMap (string,char[]); void DencryptionMap (string,char[]); void Encryption (const string,const char[],int,string&); void displayMenu (); //function prototypes void getMenuSelection (char ch); int main () { creatfile (); //creates a file to encrypt extern bool stop; while (!stop) //main program continuous loop that will display menu { displayMenu(); cin >> ch; getMenuSelection (ch); } return 0; } void creatfile () //function creates file { ofstream file; file.open ("myfile.txt"); fileRelated Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.