Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <string>
- using namespace std;
- void list();
- char validMenuOption();
- void TextToMorse();
- void Alpha(char [], int);
- void translation(char[], int);
- void MorseToText();
- int main() {
- cout << "Welcome to the morse code program" << endl;
- char choice;
- do{ // Repeat entire program for when the user doesn't choose the quit option
- choice = validMenuOption(); // Call the menu validation function
- switch (choice) // Switch between the different cases for the choice
- {
- case 'A':
- TextToMorse();
- break;
- case 'B':
- MorseToText();
- break;
- }
- }while (choice != 'C');
- return 0;
- }
- char validMenuOption(){ // Function for menu option validation
- char choice;
- list();
- cin >> choice;
- choice = toupper(choice);
- while (!(choice == 'A' || choice == 'B' || choice == 'C')) //input validation loop
- {
- list();
- cin>>choice;
- choice = toupper(choice);
- }
- return choice; // Return the choice after it's been validated
- }
- void list(){ // Function for menu list.
- cout << "Menu Options:\n"
- << "A) Text to Morse code\n"
- << "B) Morse code to text\n"
- << "C) Quit\n";
- }
- void TextToMorse(){
- char text[100];
- cout << "Enter a word and I will translate it to Morse code." << endl;
- cin.ignore();
- cin.getline(text,100);
- Alpha(text, strlen(text));
- for (int i=0;i<strlen(text);i++) {
- text[i] = toupper(text[i]);
- }
- translation(text, strlen(text));
- }
- void Alpha(char text[], int length){
- for(int i=0; i<length; i++){
- if(isalpha(text[i]) == 0){
- cout << "Error: word contains symbols" << endl;
- break;
- }
- }
- }
- void translation(char text[], int length){
- char letters[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]={".-","-...","-.-.","-..",".",
- "..-.","--.","....","..",".---","-.-",".-..","--",
- "-.","---",".--.","--.-",".-.","...","-","..-","...-",
- ".--","-..-","-.--", "--.."};
- for(int i=0; i<length; i++) {
- for (int j = 0; j<strlen(letters); j++) {
- if (letters[j] == text[i]){
- cout << morse[j] << endl;
- }
- }
- }
- }
- void MorseToText(){
- char letters[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 morse1[26]={".-","-...","-.-.","-..",".",
- "..-.","--.","....","..",".---","-.-",".-..","--",
- "-.","---",".--.","--.-",".-.","...","-","..-","...-",
- ".--","-..-","-.--", "--.."};
- char morse[100];
- cout << "Enter a Morse Code separated by /s and I will translate it to text." << endl;
- cin.ignore();
- cin.getline(morse, 100);
- string letter;
- int count = 0;
- for(int i=0; i<strlen(morse)+1; i++){
- if(morse[i] != '/' && morse[i] != '\0'){
- letter += morse[i];
- }
- if(morse[i] == '/' || morse[i] == '\0'){
- for(int j=0; j<26; j++){
- if(morse1[j] == letter){
- cout << letters[j];
- count = 0;
- }
- count++;
- }
- if(count==26){
- cout << "Error: badly formatted Morse code";
- }
- count = 0;
- letter = "";
- }
- }
- cout << endl;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement