Translating Text to Morse Code ( C++ only ) Please help me with this. Firstly, y
ID: 641613 • Letter: T
Question
Translating Text to Morse Code (C++ only)
Please help me with this.
Firstly, you need to have at least 4 functions in your program (including the main function). You need to delegate work to the various functions. The general areas of delegation are file input, character translation, and the main processing loop (where you read in data from cin and translate it to Morse code). No global variables.
Your program needs to be able to translate a character of input into the equivalent Morse code. The mappings of the characters to Morse code will be in an input file that your program will read in.
Sample Input File:
39
, --..--
. .-.-.-
? ..--..
0 -----
1 .----
2 ..---
3 ...--
4 ....-
5 .....
6 -....
7 --...
8 ---..
9 ----.
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 --..
Output:
Here is some sample output. Your output of the text file must match what is shown here for the same input. The other messages can be different, but you must have all of the needed processing:
Enter in the name of the input file that contains the mappings (strings to Morse code)
mappings.txt[Enter]
Enter the string to be translated (or quit to exit)
Starting out w/C++:[Enter]
Translates to
... - .- .-. - .. -. --. --- ..- - .-- "/" -.-. "+" "+" ":"
Enter the string to be translated (or quit to exit)
Hello world![Enter]
Translates to
.... . .-.. .-.. --- .-- --- .-. .-.. -.. "!"
Enter the string to be translated (or quit to exit)
quit[Enter]
Processing:
Your program will first ask for the name of the file that contains the mappings. The program should then open this file and read the values into two arrays. One array will contain the characters that are being mapped from and the second array will contain the equivalent Morse code. Once the arrays have been built the program will read in text from cin and translate it into Morse code. An input text that contains the lower case characters:
quit
Will indicate that the programming should stop.
I think the hardest part for me to figure out is how to use the size specified on the first line of the input file as the size of the array. And also how to separate the two arrays by character and then by translation, because everything is on one line.
Code so far:
#include <iostream>
#include <string>
#include <fstream>
#include <algorithm>
using namespace std;
//function prototype
void fileInput(string);
void translation(string, char inputArray[], char inputArr2[]);
void loop();
int main()
{
string userString1; //holds name of file name entered in by user
string userString2; //holds string to be translated entered by user
//prompt user input
cout << "Enter in the name of the input file that contains the mappings (strings to Morse code): ";
getline(cin, userString1);
fileInput(userString1);
cout << "Enter the string to be translated (or quit to exit): ";
getline(cin, userString2);
while (userString2 == "quit")
{
system("pause");
exit(1);
}
fileInput(userString1);
//displays output in Morse code
cout << "Translates to ";
system("pause");
return 0;
}
void fileInput(string userString1)
{
const int ROW = 50;
ifstream input; //allows for manipulation of input file
char inputArray[ROW]; //holds characters for each Morse code manipulation
char inputArr2[ROW]; //holds translation characters
input.open(userString1);
while (!input)
{
system("pause");
exit(1); //exits program
}
for (int x = 0; x < ROW ; x++)
{
input.get(inputArray[x]); //array holds the characters from the file
input.get(inputArr2[x]); //array holds translation characters from the file
}
//closes input file
input.close();
}
void translation(string userString2, char inputArray[], char inputArr2[])
{
transform(userString2.begin(), userString2.end(), userString2.begin(), toupper);
cout << userString2 << endl;
}
Explanation / Answer
#include <stdio.h>
void toUpperCase(char *s);
char *toMorse(char *s);
int main(int argc, char *argv[])
{
char s[]="morse code";
char *yy=NULL;
yy=toMorse(s);
printf("%s ",yy);
return 0;
}
char *toMorse(char *s)
{
char c[][37] = {
".-", /* 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*/
"-----", /* 0*/
".----", /* 1*/
"..---", /* 2*/
"...--", /* 3*/
"....-", /* 4*/
".....", /* 5*/
"-....", /* 6*/
"--...", /* 7*/
"---..", /* 8*/
"----." /* 9*/
};
char *x;
toUpperCase(s);
x=(char *)malloc(10*sizeof(char));
if(x==NULL)
{
printf("Error Allocating Memory");
exit(1);
}
while(*s)
{
if(*s>=65 && *s<=90)
{
x=(char *)realloc(x,5*sizeof(char));
if(x==NULL)
{
printf("Error Allocating Memory");
exit(1);
}
strcat(x,c[*s-65]);
strcat(x," ");
s++;
}
else if(*s>=48 && *s<=57)
{
x=(char *)realloc(x,5*sizeof(char));
if(x==NULL)
{
printf("Error Allocating Memory");
exit(1);
}
strcat(x,c[*s-22]);
strcat(x," ");
s++;
}
else if(*s==32)
{
x=(char *)realloc(x,3*sizeof(char));
if(x==NULL)
{
printf("Error Allocating Memory");
exit(1);
}
strcat(x,"/ ");
s++;
}
else
s++;
}
return x;
free(x);
}
void toUpperCase(char *s)
{
while(*s)
{
if(*s>=97 && *s<=122)
*s++-=32;
else
s++;
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.