Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Name: Noah Singer
- Date: 11/16/2019
- Section: 19
- Assignment: Morse Code Program
- Due Date: 11/17/2019
- About this project: This project translates user input from english to morse code and vice versa
- Assumptions: Assumes correct user input of a phrase or morse code
- All work below was performed by Noah Singer */
- #include <iostream>
- #include <cstring>
- #include <vector>
- using namespace std;
- //Function Prototypes
- void ShowMenu();
- char validateMenuChoice(char);
- void TextToMorse();
- void MorseToText();
- char Alphabet[26] = {'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'};
- string Morse[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
- int main()
- {
- char CHOICE; // Holds menu choice
- const char ToMorse = 'A', //Menu choice 1 -> reverse function
- ToText = 'B', //Menu choice 2 -> factorial function
- Quit = 'C'; //Menu choice 4 -> quit program
- cout << "Welcome to the morse code program" << endl;
- do
- {
- ShowMenu(); //executes ShowMenu function
- cin >> CHOICE; //accepts user input for menu choice
- CHOICE = toupper(CHOICE);
- validateMenuChoice(CHOICE); //executes validateMenuChoice function and sends choice to function
- if (CHOICE != Quit) //function terminates if user selects 4 (quit choice)
- {
- switch (CHOICE)
- {
- case ToMorse: //executes function that converts text to morse code
- TextToMorse();
- break;
- case ToText: //executes function that converts morse code to text
- MorseToText();
- break;
- }
- }
- }
- while (toupper(CHOICE) != Quit);
- return 0;
- }
- //Function to display the program menu
- void ShowMenu()
- {
- cout << "Menu Options:\n"
- << "A) Text to Morse code\n"
- << "B) Morse code to text\n"
- << "C) Quit\n";
- }
- //Validates menu choice to make sure user a letter A-C
- char validateMenuChoice (char CHOICE)
- {
- while (toupper(CHOICE) != 'A' && toupper(CHOICE) != 'B')
- {
- ShowMenu();
- cin >> CHOICE;
- }
- return CHOICE;
- }
- //Function to convert text to Morse code
- void TextToMorse()
- {
- string UserTextInput;
- cout << "Enter a word and I will translate it to Morse code.\n";
- cin.ignore();
- getline(cin, UserTextInput);
- for(unsigned int i = 0; i < UserTextInput.length(); ++i)
- UserTextInput[i] = toupper(UserTextInput[i]);
- for (int i = 0; i<UserTextInput.length(); ++i)
- {
- if (isalpha(UserTextInput.at(i)))
- {
- for (int counter = 0; counter < 26; counter++) {
- if (UserTextInput.at(i) == Alphabet[counter])
- cout << Morse[counter] << endl;
- }
- }
- else
- cout << "Error : word contains symbols" << endl;
- }
- }
- //Function to convert Morse code to text
- void MorseToText()
- {
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement