This code is used to translate morse code. I need keep this code as C style. So
ID: 3671861 • Letter: T
Question
This code is used to translate morse code. I need keep this code as C style. So please don't use C++ Style code to fix problem.
I want change this code become C style and can be run. Because when I run this code in PuTTY(Softword), it shows me there is a error in line 10. It means my softword can't read array morseAlphabet[29]. So please help me fix this problem.
This the error message is:
text.cpp: In function ‘int main()’:
text.cpp:10: warning: deprecated conversion from string constant to ‘char*’
text.cpp:10: warning: deprecated conversion from string constant to ‘char*’
The problem looks like C style array can't read "-", but can read ".".
There is a problem in array, it's real.
Please test this code in PuTTY, fix the problem and post the correct output.
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int operatingMode = 0;
char latinPhrase;
char morseBuffer[500];
char latinAlphabet[29] = { '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', '.', ',',' '};
char *morseAlphabet[29] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--", " " };
operatingMode=-1;
do{
cout << "Do you want to translate text to Morse code (1) or translate Morse code to text (2)?" << endl;
cin >> operatingMode;
}while((operatingMode != 1) && (operatingMode != 2));
cout << "Your mode of operation is " << operatingMode << endl;
if (operatingMode == 1)
{
bool FirstChar = true;
cout << "Enter Text Message: ";
cin.ignore();
while(cin.get(latinPhrase)){
bool Found=false;
int i = 0;
int j = 0;
if(FirstChar == true){
cout<<"Morse Code: ";
FirstChar = false;
}
latinPhrase = toupper(latinPhrase);
if(latinPhrase == ' '){
cout<<" ";
Found = true;
}
if(latinPhrase == ' '){
cout<<endl;
return 0;
Found = true;
}
while((Found == false) && (i < 29)){
if(latinPhrase == latinAlphabet[i]){
Found = true;
while(morseAlphabet[i][j] != 0){
cout<<morseAlphabet[i][j];
j++;
}
cout<<" ";
}
i++;
}
if(Found == false){
cout<<"You have entered an invalid character. Please try again later."<<endl;
return 0;
}
}
cout<<"End of output";
cout<<endl;
return 0;
}
if (operatingMode == 2)
{
bool FirstChar = true;
bool Found=false;
bool endofcharacter = false;
bool endofline = false;
int len;
int i;
int k = 0;
int l = 0;
cout << "Enter Morse Code: ";
cin.ignore();
while((cin.get(morseBuffer[k]))){
if(morseBuffer[k] == ' '){
morseBuffer[k] = ' ' ;
}
else if(morseBuffer[k] == ' ')
break;
else{
}
k++;
}
morseBuffer[k]=0;
len = k;
k=0;
while(morseBuffer[k] != 0){
/* scoot up to the first character */
while(morseBuffer[k] == ' '){
cout<<" ";
k++;
}
for(i = 0; i < 29; i++){
Found = true;
for(l = k; (morseBuffer[l] != ' ') &&(morseBuffer[l] != 0) ; l++)
if((morseBuffer[l] != morseAlphabet[i][l-k])){
Found = false;
}
if(((morseBuffer[l]==' ') && !(morseAlphabet[i][l-k] == 0))||
((morseBuffer[l]==0) && !(morseAlphabet[i][l-k] == 0)))
Found = false;
if(!Found)
continue;
else{
cout<<latinAlphabet[i];
k = l;
break;
}
}
}
cout<<endl;
return 0;
}
}
Explanation / Answer
#include <iostream>
#include <stdio.h>
using namespace std;
int main()
{
int operatingMode = 0;
char latinPhrase;
char morseBuffer[500];
char latinAlphabet[29] = { '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', '.', ',',' '};
char morseAlphabet[29][10] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..", ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.", "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--..", ".-.-.-", "--..--", " " };
operatingMode=-1;
do{
cout << "Do you want to translate text to Morse code (1) or translate Morse code to text (2)?" << endl;
cin >> operatingMode;
}while((operatingMode != 1) && (operatingMode != 2));
cout << "Your mode of operation is " << operatingMode << endl;
if (operatingMode == 1)
{
bool FirstChar = true;
cout << "Enter Text Message: ";
cin.ignore();
while(cin.get(latinPhrase)){
bool Found=false;
int i = 0;
int j = 0;
if(FirstChar == true){
cout<<"Morse Code: ";
FirstChar = false;
}
latinPhrase = toupper(latinPhrase);
if(latinPhrase == ' '){
cout<<" ";
Found = true;
}
if(latinPhrase == ' '){
cout<<endl;
return 0;
Found = true;
}
while((Found == false) && (i < 29)){
if(latinPhrase == latinAlphabet[i]){
Found = true;
while(morseAlphabet[i][j] != 0){
cout<<morseAlphabet[i][j];
j++;
}
cout<<" ";
}
i++;
}
if(Found == false){
cout<<"You have entered an invalid character. Please try again later."<<endl;
return 0;
}
}
cout<<"End of output";
cout<<endl;
return 0;
}
if (operatingMode == 2)
{
bool FirstChar = true;
bool Found=false;
bool endofcharacter = false;
bool endofline = false;
int len;
int i;
int k = 0;
int l = 0;
cout << "Enter Morse Code: ";
cin.ignore();
while((cin.get(morseBuffer[k]))){
if(morseBuffer[k] == ' '){
morseBuffer[k] = ' ' ;
}
else if(morseBuffer[k] == ' ')
break;
else{
}
k++;
}
morseBuffer[k]=0;
len = k;
k=0;
while(morseBuffer[k] != 0){
/* scoot up to the first character */
while(morseBuffer[k] == ' '){
cout<<" ";
k++;
}
for(i = 0; i < 29; i++){
Found = true;
for(l = k; (morseBuffer[l] != ' ') &&(morseBuffer[l] != 0) ; l++)
if((morseBuffer[l] != morseAlphabet[i][l-k])){
Found = false;
}
if(((morseBuffer[l]==' ') && !(morseAlphabet[i][l-k] == 0))||
((morseBuffer[l]==0) && !(morseAlphabet[i][l-k] == 0)))
Found = false;
if(!Found)
continue;
else{
cout<<latinAlphabet[i];
k = l;
break;
}
}
}
cout<<endl;
return 0;
}
}