In: Computer Science
Objective:
The purpose of this assignment is to:
Directions:
Write a C++ program that will create a menu driven program with the following options:
Menu Options:
A) Text to Morse code
B) Quit
If user selects A or a:
If user selects B or b:
Otherwise:
Make sure your program conforms to the following requirements:
1. This program should be called MorseCode.cpp
2. It must provide the functions defined above. You are required to use and define proper C++ functions, but for this program, you are allowed to define them (80 points).
3. Add comments wherever necessary. (5 points)
4. Run the program in linprog.cs.fsu.edu - select option A - redirect output to a file called OutputA.txt (include OutputA.txt in submission and Unix command executed) (5 points)
Sample Runs:
NOTE: not all possible runs are shown below.
Welcome to the Morse code program Menu Options: A) Text to Morse code B) Quit h Menu Options: A) Text to Morse code B) Quit a Enter a word and I will translate it to Morse code. -> sos ... --- ... Menu Options: A) Text to Morse code B) Quit A Enter a word and I will translate it to Morse code. -> ok --- -.- Menu Options: A) Text to Morse code B) Quit a Enter a word and I will translate it to Morse code. -> ?? Error : word contains symbols Menu Options: A) Text to Morse code B) Quit a Enter a word and I will translate it to Morse code. -> ok --- -.- Menu Options: A) Text to Morse code B) Quit B
General Requirements:
1) Include the header comment with your name and other information on the top of your files.
2. Please make sure that you're conforming to specifications (program name, print statements, expected inputs and outputs, etc.). Not doing so will result in a loss of points. This is especially important for prompts. They should match mine EXACTLY.
3. If we have listed a specification and allocated point for it, you will lose points if that particular item is missing from your code, even if it is trivial.
4. No global variables (variables outside of main()) unless they are constants.
5. All input and output must be done with streams, using the library iostream
6. You may only use the iostream, iomanip, vector, and string libraries. Including unnecessary libraries will result in a loss of points.
7. NO C style printing is permitted. (Aka, don't use printf). Use cout if you need to print to the screen.
8. When you write source code, it should be readable and well-documented (comments).
9. Make sure you either develop with or test with g++ (to be sure it reports no compile errors or warnings) before you submit the program.
#include <iostream>
using namespace std;
// FUNCTION PROTOTYPE
void displayMenu();
bool isAlphabet(string);
string getMorseCode(char);
void convertToMorse(string);
int main()
{
char choice;
do
{
displayMenu();
cin >> choice;
switch(choice)
{
case 'A':
case 'a':
{
string input;
cin.ignore();
cout << "Enter a word and I will translate it to Morse code.\n--> ";
getline(cin, input);
convertToMorse(input);
break;
}
case 'B':
case 'b':
{
cout << "\nThanks..Goodbye!\n\n";
exit(EXIT_SUCCESS);
}
default:
cout << "\nInvalid selection!\n\n";
}
}while(choice != 'B' || choice != 'b');
}
void displayMenu()
{
cout << "Menu Options:\nA) Text to Morse code\nB) Quit\nYour choice: ";
}
string getMorseCode(char c)
{
string res = "";
switch(c)
{
case 'a':
res = ".-";
break;
case 'b':
res = "-...";
break;
case 'c':
res = "-.-.";
break;
case 'd':
res = "-..";
break;
case 'e':
res = ".";
break;
case 'f':
res = "..-.";
break;
case 'g':
res = "--.";
break;
case 'h':
res = "....";
break;
case 'i':
res = "..";
break;
case 'j':
res = ".---";
break;
case 'k':
res = "-.-";
break;
case 'l':
res = ".-..";
break;
case 'm':
res = "--";
break;
case 'n':
res = "-.";
break;
case 'o':
res = "---";
break;
case 'p':
res = ".--.";
break;
case 'q':
res = "--.-";
break;
case 'r':
res = ".-.";
break;
case 's':
res = "...";
break;
case 't':
res = "-";
break;
case 'u':
res = "..-";
break;
case 'v':
res = "...-";
break;
case 'w':
res = ".--";
break;
case 'x':
res = "-..-";
break;
case 'y':
res = "-.--";
break;
case 'z':
res = "--..";
break;
}
return res;
}
bool isAlphabet(string s)
{
bool res = true;
for(int i = 0; i < s.length(); i++)
{
if(!(s[i] >= 'A' && s[i] <= 'Z') && !(s[i] >= 'a' && s[i] <= 'z') && s[i] != ' ')
{
res = false;
break;
}
}
return res;
}
void convertToMorse(string s)
{
string res = "";
// check if "s" contains any other characters other than the alphabets
if(!isAlphabet(s))
{
cout << "Word contains symbol\n\n";
return;
}
cout << "Morse code: ";
for(int i = 0; i < s.length(); i++)
{
if(s[i] == ' ')
res += " ";
res += getMorseCode(tolower(s[i]));
}
cout << res << endl;
}
********************************************************* SCREENSHOT *******************************************************
CODE SCREENSHOTS :



