In: Computer Science
Actual Letter ABCDEFGHIJKLMNOPQRSTUVWXYZ
Encrypted LetterBCDEFGHIJKLMNOPQRSTUVWXYZA
Deliverables: Write a C++ program containing a menu which provides the user with the following options and features:
1. Translate English Message in uppercase letters to an Encrypted Message
2. Translate Encrypted Message in uppercase letters back to an English Message
3. Quit.After translating, the user should be allowed to reselect another option from the menu. The program should keep running until the user decides to quit. Your code MUST match the sample outputs
provided in the following pages. Sample outputs, milestones and hints are on the following pages.Milestones1. Create a main() function which contains the menu and options for the user. The user should have three options:
1. Translate English Message to Encrypted Message
2. Translate Encrypted Message back to English Message
3. Quit
Remember, you can use loops and Boolean conditions to return an error and nag the user to enter the correct input. The entire main function should include a loop, allowing the user to keep translating or encoding images until the user decides to quit.
2. Declare two char arrays: one array which will hold the user’s message, and another array which will hold the output message (either the translated or encrypted version). The message size should be limited to 280 characters (the maximum size of a Twitter message). However, you never know when your boss or Twitter might change the maximum length, so it’s a good idea to declare a constant to specify your maximum message length rather than hard coding the length. You can set that constant equal to 280 for this assignment.
3. Remember, you should never assume your arrays are initialized. Remember to initialize them to zero or a dummy value before proceeding.
4. Use for loops to initialize, populate, encrypt and translate your secret messages. hint for loops which read, output, and input data to arrays.You should have the user enter zero as a flag variable to indicate the end of the message. You should have a counting variable to track the user’s message length to make it easier to output later (you don’t want to output all 280 available characters in the array if your user’s message is shorter than that). For simplicity, assume the message can only contain uppercase letters and no spaces, numbers, or special characters.If you need to encrypt or decode a message, one useful trick is to use static_cast. Each character (‘A, ‘B’, ‘C’ etc) has an integer value associated with it. For example, ‘A’ is 65, ‘B’ is 66, etc. We can use static_cast to easily increment our letters in the message in order to translate.oWhen encrypting, you want to replace the actual message with the next letter in the alphabet. You can use static cast to read the user’s message as an integer, increment the integer by 1, and then read that integer as the next letter in the alphabet:encryptedLetter = static_cast<char> (static_cast<int>(userMessage[count] + 1));oNote that for encoding letter Z, you need to set Z to A using an if statement or other means.
For translating an encrypted message back to English, the same approach can be
So, here is the solution for the given problem:
I am attaching screenshots also along with the code:
#include <iostream>
using namespace std;
const int MAX_SIZE = 280;
void encrypt();
void decrypt();
int main()
{
while(true){
cout<<"1. Translate English Message to Encrypted Message"<<endl<<"2. Translate Encrypted Message back to English Message"<<endl<<"3. Quit"<<endl;
int ch;
cin>>ch;
cin.ignore();
switch(ch){
case 1 : encrypt();
break;
case 2 : decrypt();
break;
case 3 : exit(0);
default : cout<<"\nKindly enter a valid choice\n";
}
}
return 0;
}
void encrypt(){
// for temporarily storing the string
string temp;
cin>>temp;
//taking minimum out of string length and MAX SIZE i.e 280
int len = temp.length();
if(MAX_SIZE<len)
len = MAX_SIZE;
//taking two character array
char user_msg[len] ,output_msg[len] = {'A'};
for(int i = 0;i<len;i++){
user_msg[i] = temp[i];
//if character is 0 that means this is the last character
if(temp[i] == '0'){
user_msg[i] = 0;
output_msg[i] = 0;
break;
}
//if it is less than Z increament it
else if(user_msg[i]<'Z')
output_msg[i] = (char)((int)user_msg[i] + 1);
// else make character equal to A
else
output_msg[i] = 'A';
}
cout<<"Actual message was : "<<user_msg<<endl;
cout<<"Encrypted message is : "<<output_msg<<endl;
}
void decrypt(){
//All remains same except the main loop logic
string temp;
cin>>temp;
int len = temp.length();
if(MAX_SIZE<len)
len = MAX_SIZE;
char user_msg[len] = {'A'},output_msg[len] = {'A'};
for(int i = 0;i<len;i++){
user_msg[i] = temp[i];
if(temp[i] == '0'){
user_msg[i] = 0;
output_msg[i] = 0;
break;
}
//if it is greater than A decrement it
else if(user_msg[i]>'A')
output_msg[i] = (char)((int)user_msg[i] - 1);
//else make character equal to Z
else
output_msg[i] = 'Z';
}
cout<<"Encrypted message was : "<<user_msg<<endl;
cout<<"Decrypted message is : "<<output_msg<<endl;
}
Here are the screenshots of all the functions used along with the output for your better understanding.
Output:
So, this was the solutions of the problem.
I hope I am able to solve your problem, if yes then do give it a like.
It really helps :)