Question: Write a program that asks the user if they want to encrypt or decrypt, to supply a key, and either the plaintext or ciphertext depending

Write a program that asks the user if they want to encrypt or decrypt, to supply a key, and either the plaintext or ciphertext depending on their choice to encrypt or decrypt. The program will store the user supplied string in a character vector. After which it will send the choice to encrypt/decrypt, the key, and the vector to a function that will encrypt or decrypt appropriately. Only one function will be needed for this program. Its functionality will change depending on the first parameter.

#include

#include

#include

using namespace std;

int main()

{

vector text;

string w;

int key;

char choice;

cout << "Welcome to the ceaser salad cipher machine" << endl;

cout << "Would you like to (E)ncrypt or (D)ecrypt: ";

cin >> choice;

if (choice == 'e')

{

cout << " Enter key: ";

cin >> key;

cin.clear();

cin.ignore();

cout << " Enter your plaintext: ";

getline(cin, w);

text.push_back(w);

}

else

{

if (choice == 'd')

{

cout << " Enter key: ";

cin >> key;

cin.clear();

cin.ignore();

cout << "Enter your ciphertext";

getline (cin, w);

text.push_back(w);

}

}

return 0;

}

void cipher(vector t, int &a, char &b)

{

if (b == 'e')

{

for (int i = 0, n = t.size; i < n; i++)

{

if (isalpha(t.[i]))

{

//if character is a letter, assume it is an upper case

//ASCI 65 = 'A'

int offset = 65;

//if character is a lowercase letter, offset it by 97

//ASCI 97 = 'a'

if (islower(t.[i]))

offset = 97;

//formula to encrypt the letter by the key

int cipheredLetter = (((int)t.[i] - offset + a) % 26) + offset;

//print out the encrypted character

cout << (char)cipheredLetter;

}

else //if character is not a-z or A-Z, just output it

cout << t.[i];

}

}

if (b == 'd')

{

}

}

This is all I have so far. The problem is i have tried many different ways in doing the cipher but everytime i try i get stopped by the vector.

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Databases Questions!