Question: Caesar Cipher program in C++ using 2d vectors. Need help with the encrypting loop in the encrypt function. I am trying to compare the user-input
Caesar Cipher program in C++ using 2d vectors.
Need help with the encrypting loop in the encrypt function. I am trying to compare the user-input string (message) with the characters in the first row of the 2d vector and then output the shifted message using the second row of the vector (the chars shifted using inputed key).
#include
#include
#include
using namespace std;
//function prototypes
void Encryption();
void Decryption();
void Validation();
//////////////////////
// Main Function
//////////////////////
int main()
{
cout << "Welcome to Cryptography ";
Validation();
return 0;
}
/////////////////////////////////////////
// Function Name: Validation
// Parameters:
// Purpose:
/////////////////////////////////////////
void Validation()
{
string choice;
const string encrypt = "encrypt";
const string decrypt = "decrypt";
const string exit = "exit";
bool flag;
//do while loop to send direction to which function
do
{
cout << "What do you wish to do with your message? "
"(encrypt, decrypt, exit) ";
cout << "Enter choice here: ";
cin >> choice;
if (choice == encrypt)
{
cout << " you have chosen encrypt ";
Encryption();
break;
}
else if (choice == decrypt)
{
cout << " you have chosen decrypt ";
Decryption();
break;
}
else if (choice == exit)
{
cout << " you have chosen to leave, goodbye! ";
flag = false;
}
else
{
cout << "(ERROR: INVALID CHOICE) ";
flag = true;
}
//to exit the loop for now
//flag = false;
} while (flag);
}
/////////////////////////////////////////
// Function Name: Encryption
// Parameters:
// Return: the encrypted string
// Purpose:
/////////////////////////////////////////
void Encryption()
{
string message;
string encrypted_message;
string encryption_result = "";
int key;
cout << " you are now inside the function encryption ";
cout << "Enter your message: ";
cin >> message;
//Capitalizes each letter in message.
for(int i = 0; i < message.size(); i++)
{
message[i] = toupper(message[i]);
}
cout << message << endl;
cout << "Enter the key number (1-35) ";
cin >> key;
cin.ignore();
while(key < 1 || key > 35)
{
cout << "Invalid Key Number. ";
Validation();
}
//Encryption process
char array[36] = {'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','0','1','2','3','4','5','6','7','8','9'};
vector<char> orig(array, array + 36);
vectorchar> > encrypt(2, orig);
//creates shifted second row in vector
int k = 0;
for(int i = 0; i < 36; i++)
{
k = (i+key)%36;
encrypt[1][i] = encrypt[1][k];
}
for (int z = 0; z < 36; z++)
{
encrypt[1][z+36-key] = encrypt[0][z];
}
//*******************************************************************
//Encrypts message (part that I need help with)
for(int v = 0; v < message.size(); v++)
{
//Code to compare string input to 2d vector and
//encrypt message 'key' places.
}
for(int v = 0; v < message.size(); v++)
{
cout << message[v];
}
cout << endl << endl;
Validation();
}
Step by Step Solution
There are 3 Steps involved in it
Get step-by-step solutions from verified subject matter experts
