G2A Many GEOs
SHARE
TWEET

Untitled

a guest Apr 7th, 2020 180 Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string>
  4.  
  5. using namespace std;
  6.  
  7. void list();
  8. char validMenuOption();
  9. void TextToMorse();
  10. void Alpha(char [], int);
  11. void translation(char[], int);
  12. void MorseToText();
  13.  
  14. int main() {
  15.  
  16.     cout << "Welcome to the morse code program" << endl;
  17.     char choice;
  18.     do{ // Repeat entire program for when the user doesn't choose the quit option
  19.         choice = validMenuOption(); // Call the menu validation function
  20.         switch (choice) // Switch between the different cases for the choice
  21.         {
  22.             case 'A':
  23.                 TextToMorse();
  24.                 break;
  25.             case 'B':
  26.                 MorseToText();
  27.                 break;
  28.         }
  29.     }while (choice != 'C');
  30.     return 0;
  31. }
  32. char validMenuOption(){ // Function for menu option validation
  33.     char choice;
  34.     list();
  35.     cin >> choice;
  36.     choice = toupper(choice);
  37.     while (!(choice == 'A' || choice == 'B' || choice == 'C')) //input validation loop
  38.     {
  39.         list();
  40.         cin>>choice;
  41.         choice = toupper(choice);
  42.     }
  43.     return choice; // Return the choice after it's been validated
  44. }
  45. void list(){ // Function for menu list.
  46.     cout << "Menu Options:\n"
  47.          << "A) Text to Morse code\n"
  48.          << "B) Morse code to text\n"
  49.          << "C) Quit\n";
  50. }
  51. void TextToMorse(){
  52.     char text[100];
  53.     cout << "Enter a word and I will translate it to Morse code." << endl;
  54.     cin.ignore();
  55.     cin.getline(text,100);
  56.     Alpha(text, strlen(text));
  57.     for (int i=0;i<strlen(text);i++) {
  58.         text[i] = toupper(text[i]);
  59.     }
  60.     translation(text, strlen(text));
  61. }
  62. void Alpha(char text[], int length){
  63.     for(int i=0; i<length; i++){
  64.         if(isalpha(text[i]) == 0){
  65.             cout << "Error: word contains symbols" << endl;
  66.             break;
  67.         }
  68.     }
  69. }
  70. void translation(char text[], int length){
  71.     char letters[26]={'A','B','C','D','E','F','G',
  72.                           'H','I','J','K','L','M','N','O','P','Q','R',
  73.                           'S','T','U','V','W','X','Y','Z'};
  74.     string morse[26]={".-","-...","-.-.","-..",".",
  75.                          "..-.","--.","....","..",".---","-.-",".-..","--",
  76.                          "-.","---",".--.","--.-",".-.","...","-","..-","...-",
  77.                          ".--","-..-","-.--", "--.."};
  78.     for(int i=0; i<length; i++) {
  79.         for (int j = 0; j<strlen(letters); j++) {
  80.         if (letters[j] == text[i]){
  81.             cout << morse[j] << endl;
  82.         }
  83.     }
  84.     }
  85. }
  86. void MorseToText(){
  87.     char letters[26]={'A','B','C','D','E','F','G',
  88.                       'H','I','J','K','L','M','N','O','P','Q','R',
  89.                       'S','T','U','V','W','X','Y','Z'};
  90.     string morse1[26]={".-","-...","-.-.","-..",".",
  91.                        "..-.","--.","....","..",".---","-.-",".-..","--",
  92.                        "-.","---",".--.","--.-",".-.","...","-","..-","...-",
  93.                        ".--","-..-","-.--", "--.."};
  94.     char morse[100];
  95.     cout << "Enter a Morse Code separated by /s and I will translate it to text." << endl;
  96.     cin.ignore();
  97.     cin.getline(morse, 100);
  98.     string letter;
  99.     int count = 0;
  100.     for(int i=0; i<strlen(morse)+1; i++){
  101.         if(morse[i] != '/' && morse[i] != '\0'){
  102.             letter += morse[i];
  103.         }
  104.         if(morse[i] == '/' || morse[i] == '\0'){
  105.             for(int j=0; j<26; j++){
  106.                 if(morse1[j] == letter){
  107.                     cout << letters[j];
  108.                     count = 0;
  109.                 }
  110.                 count++;
  111.             }
  112.             if(count==26){
  113.                 cout << "Error: badly formatted Morse code";
  114.             }
  115.             count = 0;
  116.             letter = "";
  117.         }
  118.     }
  119.     cout << endl;
  120. }
RAW Paste Data
We use cookies for various purposes including analytics. By continuing to use Pastebin, you agree to our use of cookies as described in the Cookies Policy. OK, I Understand
Top