I'm trying to implement a program that reads a text file that has a caesar cipher encryption. My program's purpose is to decode it. With my code it only reads the file and doesnt decode anything. Where am I going wrong? It echoprints the file but it doesnt decode it. I think my logic is flawed at the end of the CaesarCipher function
#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 CaesarCipher (ifstream &); //prototype for performing a caesar decryption
//void SubCipher (ifstream &); //prototype for performing a substitution decryption
const int ALPHA_SIZE = 26;
typedef char AlphaArray[ALPHA_SIZE]; //alphabet array with 26 elements
AlphaArray realAlphas;
AlphaArray encryptionArray;
ifstream inFile;
int main()
{
char choice; //menu choice from user
ifstream inFile; //opens file
string fileName; //user inputs name of file
bool condition = true;
PrintHeading(); // prints heading
while(condition)
{
choice = PrintMenu(); //passes choice to printMenu function
switch(choice)
{
case 'C': //choice for caesar substitution
case 'c': cout << choice << endl;
OpenFile(inFile);
CaesarCipher (inFile);
break;
case 'S': //choice for substitution cipher
case 's': cout << choice << endl;
OpenFile(inFile);
//SubCipher(inFile);
break;
case 'Q': //choice for quitting the program
case 'q': condition = false; //prints closing message when false
cout << "***********************************************" << endl
<< "\tThank you for using the DECRYPTER!" << endl
<< "***********************************************" << endl << endl;
break;
default:
//default statement for incorrect input
cout << "Not a valid choice. Try again." << endl;
break;
}
}
return 0;
}
void PrintHeading () //prints heading for decryption program
{
cout << "***********************************************" << endl
<< "\t Welcome to The DECRYPTER!\t\t" << endl
<< endl
<< " You have the option of performing either a" << endl
<< "\t Caesar decryption or " << endl
<< "\t a Substitution decryption" << endl
<< endl
<< " You will be asked for a file to be decoded." << endl
<< endl
<< " The DECRYPTER will then echoprint one line " << endl
<< " of your encoded text from your specified file" <<endl
<< " followed by the same text decoded." << endl << endl
<< "***********************************************" << endl
<< endl
<< endl;
}
char PrintMenu () //function for displaying menu and gathering user's input
{
char userChoice; //menu choice from user
//menu for user input
cout << "What would you like to do?" << endl
<< "To decode a text file using the Caesar cipher, press c" << endl
<< "To decode a text file using the substitution cipher, press s" << endl
<< "To quit decoding, press q" << endl
<< "What is your selection ? ";
cin >> userChoice;
while(userChoice != 'C' && userChoice != 'c' && userChoice != 'S' && userChoice != 's' && userChoice != 'Q' && userChoice != 'q')
{
cout << "Please re-enter a valid menu choice: "; //prompts user to input choice until it is valid
cin >> userChoice;
}
return userChoice; //return user's menu choice
}
void OpenFile (ifstream & infile)
{
string fileName; //file input from user
cout << endl;
cout << "Please enter the file name to decode -> " << endl; //asks for file name from user
cin >> fileName; //user file name
inFile.clear();
inFile.open(fileName.c_str());
while (!inFile) //loop for if file doesnt exist
{
cout << "File doesn't exist. Try again: " << endl;
cin >> fileName;
void CaesarCipher (ifstream & infile)
{
char ch, temp;
for (int i = 0; i < 26; i++)
{
realAlphas[i] = 65 + i;
}
int caesarConstant = 4;
for (int i = 0; i < 26; i++)
{
encryptionArray[i] = realAlphas[(i + caesarConstant) % ALPHA_SIZE];
}
cout << "Original alphabet: " << realAlphas << endl;
cout << "Encrypted alphabet: " << encryptionArray << endl;
cout << endl;
int i;
while (inFile)
{
if (!inFile.eof())
{
ch = inFile.get();
cout << ch;
for (i = 0; i < 26; i++)
{
encryptionArray[i] = realAlphas[i];
}
if (islower(encryptionArray[i]))
{
encryptionArray[i] = (encryptionArray[i] - 'a' + caesarConstant) % 26 + 'a';
} else if (isupper(encryptionArray[i])) {
encryptionArray[i] = (encryptionArray[i] - 'A' + caesarConstant) % 26 + 'A';
}
}
}
}
Aucun commentaire:
Enregistrer un commentaire