Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- 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;
- }
- 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(){
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement