Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

Lang: C++ Need help starting/outlining code: Problem: You will write a program t

ID: 3601363 • Letter: L

Question

Lang: C++

Need help starting/outlining code:

Problem:

You will write a program that asks the user for a string of characters and prints the encrypted message back to the user as a string of ASCII values in base 2 OR asks the user for a string of ASCII values in base 2 and prints the decrypted string of characters corresponding to those values back to the user. This program will use functions for converting numbers, getting input from the user, converting input from the user, and error checking. Ask the user if they want to encrypt a message entered as string of text or decrypt a message given as a string of ASCII characters in base 2. If the user wants to encrypt a message of text, then read a line of text that can include spaces, e.g. “jen” or “jen mocello”. o Print the user’s encrypted message back to the user in binary. If the user wants to decrypt a message of characters in binary, then read a string of 1s and 0s from the user. o Print the decrypted message in corresponding text. Continue to prompt the user for encrypting or decrypting until the user doesn’t want to do this anymore. Print an error message when: The encrypted message (binary) cannot be separated into bytes The encrypted message (binary) isn’t 1s and 0s Since our messages are in English, we are not using the extended ASCII chart (128-255). Make sure the binary numbers will produce characters within 0-127. Recover from the error by re-prompting the user for a new binary number after their error, until they give a good binary number. Example: Do you want to encrypt or decrypt a message: encrypt Enter the string to encrypt: jen 011010100110010101101110 Do you want to play again (yes or no)? yes Do you want to encrypt or decrypt a message: decrypt Enter the string to encrypt: 011010100110010101101110 jen Do you want to play again (yes or no)? no

Explanation / Answer

Please find the C++ code below:

#include <string>

#include <bitset>

#include <iostream>

using namespace std;

//Function to encrypt the input string from ASCII to binary (base 2)

void encrypt(string myString)

{

//Print the ASCII for each character of the string

for (std::size_t i = 0; i < myString.size(); ++i)

cout << bitset<8> (myString.c_str()[i]) ;

cout << endl;

}

//Function to decrypt the binary (base 2 ) string to ASCII

void decrypt(string myString)

{

char ic;

bitset<8> oc;

int i = 8;

for(int j=0;j<myString.size();j++){

ic=myString.at(j);

oc[--i] = ic - '0';

if (i == 0)

{

cout << static_cast<char>(oc.to_ulong());

i = 8;

}

}

cout << endl;

}

//Driver program to test the functions

int main()

{

int ch=0;

string str;

while (ch!=3)

{

cout << "1. Encrypt String" << endl;

cout << "2. Decrypt String" << endl;

cout << "3. Quit" << endl;

cout << "Enter your choice: ";

cin >> ch;

if (ch == 1)

{

cout << "Enter string: ";

cin >> str;

encrypt(str);

}

else if (ch == 2)

{

cout << "Enter binary string: ";

cin >> str;

decrypt(str);

}

}

return 0;

}

Output:

1. Encrypt String
2. Decrypt String
3. Quit
Enter your choice: 1
Enter string: jen
011010100110010101101110
1. Encrypt String
2. Decrypt String
3. Quit
Enter your choice: 2
Enter binary string: 011010100110010101101110
jen
1. Encrypt String
2. Decrypt String
3. Quit
Enter your choice: 3