Advertisement
Guest User

Untitled

a guest
Apr 6th, 2020
174
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 2.48 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3.  
  4. using namespace std;
  5.  
  6. void list();
  7. char validMenuOption();
  8. void TextToMorse();
  9. void Alpha(char [], int);
  10. void translation(char[], int);
  11. void MorseToText();
  12.  
  13. int main() {
  14.  
  15.     cout << "Welcome to the morse code program" << endl;
  16.     char choice;
  17.     do{ // Repeat entire program for when the user doesn't choose the quit option
  18.         choice = validMenuOption(); // Call the menu validation function
  19.         switch (choice) // Switch between the different cases for the choice
  20.         {
  21.             case 'A':
  22.                 TextToMorse();
  23.                 break;
  24.             case 'B':
  25.                 MorseToText();
  26.                 break;
  27.         }
  28.     }while (choice != 'C');
  29.     return 0;
  30. }
  31. char validMenuOption(){ // Function for menu option validation
  32.     char choice;
  33.     list();
  34.     cin >> choice;
  35.     choice = toupper(choice);
  36.     while (!(choice == 'A' || choice == 'B' || choice == 'C')) //input validation loop
  37.     {
  38.         list();
  39.         cin>>choice;
  40.     }
  41.     return choice; // Return the choice after it's been validated
  42. }
  43. void list(){ // Function for menu list.
  44.     cout << "Menu Options:\n"
  45.          << "A) Text to Morse code\n"
  46.          << "B) Morse code to text\n"
  47.          << "C) Quit\n";
  48. }
  49. void TextToMorse(){
  50.     char text[100];
  51.     cout << "Enter a word and I will translate it to Morse code." << endl;
  52.     cin.ignore();
  53.     cin.getline(text,100);
  54.     Alpha(text, strlen(text));
  55.     for (int i=0;i<strlen(text);i++) {
  56.         text[i] = toupper(text[i]);
  57.     }
  58.     translation(text, strlen(text));
  59. }
  60. void Alpha(char text[], int length){
  61.     for(int i=0; i<length; i++){
  62.         if(isalpha(text[i]) == 0){
  63.             cout << "Error: word contains symbols" << endl;
  64.             break;
  65.         }
  66.     }
  67. }
  68. void translation(char text[], int length){
  69.     char letters[26]={'a','b','c','d','e','f','g',
  70.                           'h','i','j','k','l','m','n','o','p','q','r',
  71.                           's','t','u','v','w','x','y', 'z'};
  72.     string morse[26]={".-","-...","-.-.","-..",".",
  73.                          "..-.","--.","....","..",".---","-.-",".-..","--",
  74.                          "-.","---",".--.","--.-",".-.","...","-","..-","...-",
  75.                          ".--","-..-","-.--", "--.."};
  76.     for(int i=0; i<length; i++) {
  77.         for (int j = 0; j<strlen(letters); j++) {
  78.         if (letters[j] == text[i]){
  79.             cout << morse[j] << endl;
  80.         }
  81.     }
  82.     }
  83. }
  84. void MorseToText(){
  85.  
  86. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement