Advertisement
Guest User

Untitled

a guest
Nov 17th, 2019
119
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.15 KB | None | 0 0
  1. /* Name: Noah Singer
  2. Date: 11/16/2019
  3. Section: 19
  4. Assignment: Morse Code Program
  5. Due Date: 11/17/2019
  6. About this project: This project translates user input from english to morse code and vice versa
  7. Assumptions: Assumes correct user input of a phrase or morse code
  8. All work below was performed by Noah Singer */
  9.  
  10. #include <iostream>
  11. #include <cstring>
  12. #include <vector>
  13.  
  14. using namespace std;
  15.  
  16. //Function Prototypes
  17. void ShowMenu();
  18. char validateMenuChoice(char);
  19. void TextToMorse();
  20. void MorseToText();
  21.  
  22. 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'};
  23. string Morse[26] = {".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.."};
  24.  
  25. int main()
  26. {
  27. char CHOICE; // Holds menu choice
  28. const char ToMorse = 'A', //Menu choice 1 -> reverse function
  29. ToText = 'B', //Menu choice 2 -> factorial function
  30. Quit = 'C'; //Menu choice 4 -> quit program
  31. cout << "Welcome to the morse code program" << endl;
  32. do
  33. {
  34. ShowMenu(); //executes ShowMenu function
  35. cin >> CHOICE; //accepts user input for menu choice
  36. CHOICE = toupper(CHOICE);
  37. validateMenuChoice(CHOICE); //executes validateMenuChoice function and sends choice to function
  38. if (CHOICE != Quit) //function terminates if user selects 4 (quit choice)
  39. {
  40. switch (CHOICE)
  41. {
  42. case ToMorse: //executes function that converts text to morse code
  43. TextToMorse();
  44. break;
  45. case ToText: //executes function that converts morse code to text
  46. MorseToText();
  47. break;
  48. }
  49. }
  50. }
  51. while (toupper(CHOICE) != Quit);
  52. return 0;
  53. }
  54.  
  55. //Function to display the program menu
  56. void ShowMenu()
  57. {
  58. cout << "Menu Options:\n"
  59. << "A) Text to Morse code\n"
  60. << "B) Morse code to text\n"
  61. << "C) Quit\n";
  62. }
  63. //Validates menu choice to make sure user a letter A-C
  64. char validateMenuChoice (char CHOICE)
  65. {
  66. while (toupper(CHOICE) != 'A' && toupper(CHOICE) != 'B')
  67. {
  68. ShowMenu();
  69. cin >> CHOICE;
  70. }
  71. return CHOICE;
  72. }
  73. //Function to convert text to Morse code
  74. void TextToMorse()
  75. {
  76. string UserTextInput;
  77. cout << "Enter a word and I will translate it to Morse code.\n";
  78. cin.ignore();
  79. getline(cin, UserTextInput);
  80. for(unsigned int i = 0; i < UserTextInput.length(); ++i)
  81. UserTextInput[i] = toupper(UserTextInput[i]);
  82. for (int i = 0; i<UserTextInput.length(); ++i)
  83. {
  84. if (isalpha(UserTextInput.at(i)))
  85. {
  86. for (int counter = 0; counter < 26; counter++) {
  87. if (UserTextInput.at(i) == Alphabet[counter])
  88. cout << Morse[counter] << endl;
  89. }
  90. }
  91. else
  92. cout << "Error : word contains symbols" << endl;
  93. }
  94. }
  95. //Function to convert Morse code to text
  96. void MorseToText()
  97. {
  98.  
  99. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement