1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158
|
#include <iostream> //for standard I/O
#include <fstream> //necessary for file input
#include <string> //necessary for fileName input
using namespace std;
void PrintHeading (); //prototype for printing the initial heading
char PrintMenu (); //prototype for printing menu and gaining user's choice
void OpenFile (ifstream &); //prototype for opening file
void CeasarCypher (ifstream &); //prototype for performing a caesar decryption
void SubCypher (ifstream &); //prototype for performing a substitution decryption
typedef char alphabet[26]; //alphabet array with 26 elements
int main()
{
char choice; //input from user's menu choice
ifstream inFile; //necessary for opening file
string fileName; //needed for user to input name of file
bool condition = true;
//call function that prints overall heading
PrintHeading();
while(condition)
{
choice = PrintMenu(); //assign char from PrintMenu function to choice variable
switch(choice)
{
case 'C':
case 'c': cout << choice << endl;
OpenFile(inFile);
CeasarCypher(inFile);
break;
case 'S':
case 's': cout << choice << endl;
OpenFile(inFile);
break;
case 'Q': //if user wants to quit program
case 'q': condition = false; //set condition to false and print closing message
cout << "***********************************************" << endl
<< "\tThank you for using the DECRYPTER!" << endl
<< "***********************************************" << endl << endl;
break;
default: cout << "No." << endl;
break;
}
}
return 0; //return zero
}
void PrintHeading () //function that prints initial heading of decryption program
{ //PrintHeading function
cout << "***********************************************" << endl
<< "\t Welcome to The DECRYPTER!\t\t" << endl << endl
<< " You have the option of performing either a" << endl
<< "\t Caesar decryption\t" << endl << "\t\t or \t\t" << endl
<< "\tSubstitution decryption.\t" << endl << endl
<< " You will be asked for a file to be decoded." << endl << endl
<< " The DECRYPTER will then echoprint one line of" << endl
<< " your encoded text from your specified file" <<endl
<< " followed by the same text decoded." << endl << endl
<< "\t Let's get started!" << endl
<< "***********************************************" << endl << endl << endl;
} //PrintHeading function
char PrintMenu () //function that displays menu and gathers user's choice as input
{ //PrintMenu function
char menuChoice; //character that user inputs as menu choice
//prompt user for selection and gather input
cout << "Which decryption method would you like to use?" << endl
<< "To use the Caesar Cypher, please enter C." << endl
<< "To use the Substitution Cypher, enter S." << endl
<< "To quit the program, enter Q." << endl
<< "Please enter your selection: ";
cin >> menuChoice;
while(menuChoice != 'C' && menuChoice != 'c' && menuChoice != 'S' && menuChoice != 's' && menuChoice != 'Q' && menuChoice != 'q')
{
cout << "Please re-enter a valid menu choice: "; //if selecition is not a valid menu choice
cin.clear(); //clear and reprompt until valid choice is obtained
cin >> menuChoice;
}
return(menuChoice); //return user's menu choice
} //PrintMenu functionss
void OpenFile (ifstream & inFile)
{
string fileName;
cout << endl << "Please enter a valid file name (no blanks!) to decode: "; //prompt for file name to be decrypted
cin >> fileName; //user inputs string for name of file
inFile.open(fileName.c_str()); //if file exists, open it
while(!inFile) //if file does not exist
{ //reprompt user until they input valid file name
cout << "Sorry! That is not a valid file name! Please try again: ";
cin >> fileName;
inFile.clear();
inFile.open(fileName.c_str());
}
}
void CaesarCypher (ifstream & inFile)
{
alphabet normal = {'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H',
'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P',
'Q', 'R', 'S', 'T', 'U', 'V', 'W','X',
'Y', 'Z'};
alphabet caesar;
int digitcount = 0,linecount = 0,shift;
char shiftval,decchar;
bool matchfound = false;
string temp;
inFile.get(shiftval);
if(inFile.peek() != '\n')
{
shift = (shiftval - '0')*10;
inFile.get(shiftval);
shift= shift + (shiftval - '0');
}
for(int i =0; i < 26; i++)
{
caesar[i] = normal[(i+shift)%26]; //make sure this gives correct alphabet values
}
//Print out heading here
while(getline(inFile, temp))
{
cout << temp;
for(int i = 0; i < temp.size(); i++)
{
for(int j = 0; j < 26; j++)
{
if(temp[i] == caesar[j])
{
decchar = normal[j];
matchfound = true;
}
}
if(!matchfound)
cout << temp[i];
else
cout << decchar;
}
cout << endl;
decchar = ' ';
matchfound = false;
}
inFile.close();
}
|