Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Task Description: The program in CombinationCipher.cpp takes a message written by the user and puts it through multiple ciphers. A cipher is a technique to

Task Description: The program in CombinationCipher.cpp takes a message written by the user and puts it through multiple ciphers. A cipher is a technique to create a secret code from a message. A combination cipher uses multiple ciphers to encode the same message. This combination cipher uses a Keyboard cipher twice and then uses Morse code. The Keyboard Code is just the order of letters your keyboard. The top row is the original letter, bottom row is the encoded letter. 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 Q W E R T Y U I O P A S D F G H J K L Z X C V B N M The program in CombinationCipher.cpp file works, but it is ugly and harder to understand than it should be. Thats because it was written without functions other than main(). As a consequence, a lot of code gets repeated, and the overall logic of the program is hidden by the mass of details. Rewrite the program by grouping the code into functions. Change the program so it takes the input from a file input.txt instead of the terminal. In particular, your program should include the following functions: 1. A function named readInput that returns a string containing the message from a file. The parameter should be an fstream passed by reference. 2. A function named upperCase that returns nothing (void). The parameter should be one string containing the user entered message that gets modified to be all upper case letters. Hint: pass by reference. 3. A function named cipher that returns a string containing the fully encoded message. The parameter should be one string containing the uppercase user entered message. 4. A function named translateLetterKeyBoard that returns nothing (void). The parameter should be one char containing the letter to be modified, in accordance to the Keyboard cipher. Hint: pass by reference. 5. A function named translateLetterMorseCode that returns a string containing the character in morse code. The parameter should be one char containing the letter to be translated, in accordance to Morse code. 6. A function named printOutput that returns nothing (void). The parameters should be two strings, the original user entered message and the final encoded message. This function should print the output. As you introduce each function, replace the code in main() by calls to your new functions as appropriate. In particular, note that some of these new functions may be called from within the bodies of some of the other functions. Remember that this program was already working. You should not alter the output of the program in any way. Hopefully, once you are done it will be obvious to you that your revised code is simpler and easier to understand than the original. In real life, code that is easier to understand, is easier to get working in the first place, so you should try to develop the code in small, function-based chunks from the very beginning. To test your code you can always run inputs through the original .cpp and your own to compare the outputs.

#include  #include  using namespace std; int main() { cout << "Assignment 0- Combination Cipher "; //Prompt the user for a string string userInput =""; cout << "Enter the string you would like encoded: "; getline(cin, userInput); string tempMessage =userInput; //clean up the message and make all upper case for(int i = 0; i < tempMessage.size(); i++) tempMessage[i] = toupper(tempMessage[i]); //Encode the message through KeyBoard Cipher for(int i = 0; i < tempMessage.size(); i++){ char letter = tempMessage[i]; switch (letter){ case 'A': letter = 'Q'; break; case 'B': letter = 'W'; break; case 'C': letter = 'E'; break; case 'D': letter = 'R'; break; case 'E': letter = 'T'; break; case 'F': letter = 'Y'; break; case 'G': letter = 'U'; break; case 'H': letter = 'I'; break; case 'I': letter = 'O'; break; case 'J': letter = 'P'; break; case 'K': letter = 'A'; break; case 'L': letter = 'S'; break; case 'M': letter = 'D'; break; case 'N': letter = 'F'; break; case 'O': letter = 'G'; break; case 'P': letter = 'H'; break; case 'Q': letter = 'J'; break; case 'R': letter = 'K'; break; case 'S': letter = 'L'; break; case 'T': letter = 'Z'; break; case 'U': letter = 'X'; break; case 'V': letter = 'C'; break; case 'W': letter = 'V'; break; case 'X': letter = 'B'; break; case 'Y': letter = 'N'; break; case 'Z': letter = 'M'; break; default : break; } tempMessage[i] = letter; } //Encode the message through KeyBoard Cipher a second time for(int i = 0; i < tempMessage.size(); i++){ char letter = tempMessage[i]; switch (letter){ case 'A': letter = 'Q'; break; case 'B': letter = 'W'; break; case 'C': letter = 'E'; break; case 'D': letter = 'R'; break; case 'E': letter = 'T'; break; case 'F': letter = 'Y'; break; case 'G': letter = 'U'; break; case 'H': letter = 'I'; break; case 'I': letter = 'O'; break; case 'J': letter = 'P'; break; case 'K': letter = 'A'; break; case 'L': letter = 'S'; break; case 'M': letter = 'D'; break; case 'N': letter = 'F'; break; case 'O': letter = 'G'; break; case 'P': letter = 'H'; break; case 'Q': letter = 'J'; break; case 'R': letter = 'K'; break; case 'S': letter = 'L'; break; case 'T': letter = 'Z'; break; case 'U': letter = 'X'; break; case 'V': letter = 'C'; break; case 'W': letter = 'V'; break; case 'X': letter = 'B'; break; case 'Y': letter = 'N'; break; case 'Z': letter = 'M'; break; default : break; } tempMessage[i] = letter; } //encode the message through Morse code string encodedMessage = ""; for(int i = 0; i < tempMessage.size(); i++){ string output = ""; char letter = tempMessage[i]; switch(letter){ case 'A': output = ".- "; break; case 'B': output = "-... "; break; case 'C': output = "-.-. "; break; case 'D': output = "-.. "; break; case 'E': output = ". "; break; case 'F': output = "..-. "; break; case 'G': output = "--. "; break; case 'H': output = ".... "; break; case 'I': output = ".. "; break; case 'J': output = ".--- "; break; case 'K': output = "-.- "; break; case 'L': output = ".-.. "; break; case 'M': output = "-- "; break; case 'N': output = "-. "; break; case 'O': output = "--- "; break; case 'P': output = ".--. "; break; case 'Q': output = "--.- "; break; case 'R': output = ".-. "; break; case 'S': output = "... "; break; case 'T': output = "- "; break; case 'U': output = "..- "; break; case 'V': output = "...- "; break; case 'W': output = ".-- "; break; case 'X': output = ".-- "; break; case 'Y': output = "-.-- "; break; case 'Z': output = "--.. "; break; case ' ': output = " "; break; case '1': output = ".---- "; break; case '2': output = "..--- "; break; case '3': output = "...-- "; break; case '4': output = "....- "; break; case '5': output = "..... "; break; case '6': output = "-.... "; break; case '7': output = "--... "; break; case '8': output = "---.. "; break; case '9': output = "----. "; break; case '0': output = "----- "; break; default: output = letter; } encodedMessage += output; } //Print the output cout << endl << "You Gave me the Message: " << endl << userInput << endl << endl << "Your Message Encoded is: " << endl << encodedMessage << endl; return 0; }

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image_2

Step: 3

blur-text-image_3

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

More Books

Students also viewed these Databases questions

Question

a donor to the university or school?

Answered: 1 week ago