The morse to alpha isn\'t working and I can only get single-words to be translat
ID: 3673064 • Letter: T
Question
The morse to alpha isn't working and I can only get single-words to be translate. Any help would be greatly appreciated!
#include <iostream>
#include <cstdlib>
#include <istream>
using namespace std;
/***************************************************************
Function:strlen
Description: finds the length of something
Parameters:text
Return:length
Pre-Conditions: reading in a pointer
Post-Conditions:coming up the length of th input
*************************************************************/
size_t strlen(char *str)
{
size_t len = 0;
while (*str != '')
{
str++;
len++;
}
return len;
}
/*************************************************************
Function:strcat
Description:appends the pointer
Parameters:pointers
Return: pointer to the resulting destination
Pre-Conditions: pointer in the source array
Post-Conditions:pointer in the destination array
**************************************************************/
char* strcat(const char *destination, const char *source)
{
unsigned destination_length = strlen(destination),
combined_length = destination_length + strlen(source);
char *combined_string = new char[combined_length + 1];
for (unsigned i = 0; i < combined_length; i++)
{
if (i < destination_length)
combined_string[i] = destination[i];
else
combined_string[i] = source[i - destination_length];
}
combined_string[combined_length] = '';
return combined_string;
}
/*****************************************************
Function:strcat
Description:appends the pointer
Parameters:pointers
Return: pointer to the resulting destination
Pre-Conditions: char in the source array
Post-Conditions:char in the destination array
********************************************************/
char* strcat(const char *destination, const char source)
{
unsigned destination_length = strlen(destination),
combined_length = destination_length;
char *combined_string = new char[combined_length + 1];
for (unsigned i=0; i < combined_length; i++)
{
if(i < destination_length)
combined_string[i] = destination[i];
else
combined_string[i] = source;
}
combined_string[combined_length] = '';
return combined_string;
}
/**************************************************************
Function:engtomol
Description: converts the text into morse code
Parameters:text
Return:output
Pre-Conditions: all parameters are valid variables in morse
Post-Conditions:the morse code will be printed to the screen
***************************************************************/
char* engtomol(char* text)
{
const char* morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
char alpha_num[36]={'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'};
size_t const characters = 36;
char* output= new char[255];
output[0]='';
size_t index = 0;
size_t const size = strlen(text);
while (index < size)
{
int position = 0;
if (text[index] != ' ')
{
while (alpha_num[position] != toupper(text[index]) && position < 36)
{
++position;
}
output=strcat(output,morse_code[position]);
if(index != size - 1)
{
output=strcat(output," ");
}
}
else
{
output=strcat(output," ");
}
index++;
}
return output;
}
/**************************************************************
Function:moltoeng
Description: converts the morse into letters
Parameters:morse
Return:output
Pre-Conditions: all parameters are valid morse variables
Post-Conditions:the letters will be printed to the screen
***************************************************************/
char* moltoeng(char* morsecode)
{
const char* morse_code[36] = { ".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----." };
char alpha_num[36] = {'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' };
size_t const characters = 36;
char* output = new char[255];
output[0]='';
size_t index = 0;
char* letter = new char[36];
size_t const size = sizeof(morsecode);
while (index < size)
{
while (morsecode[index] != ' ' && index < size)
{
letter += morsecode[index++];
}
//find this code in the morse alphabet
int position = 0;
while (morse_code[position] != letter && position < 36)
{
++position;
}
output += alpha_num[position];
int count = 0;
while (morsecode[index] == ' '&& index < size)
{
index++;
count++;
}
output=strcat(output,alpha_num[index]);
if (count >= 7)
{
output = strcat(output," ");
}
}
return output;
}
int main()
{
char* text = new char[255];
char* morsecode = new char[255];
int choice = 0;
char repeat = 'y';
while (repeat == 'y')
{
cout<<"do you want to translate English to Morse code(1) or translate Morse code to English (2)?"<<endl;
cin>>choice;
if(choice==1)
{
cout<<"you have selected English to morse." << endl;
cout<<"Please type in a text you would like translated "<< endl;
cin>>text;
cout<< "Text:" << text << endl;
cout<< "Morsecode: " << engtomol(text) <<endl;
}
else if(choice==2)
{
cout<<"You have selected morse to english." << endl;
cout<<"Please type in morse code(1 space between . and - within a character, 3 spaces between characters and 7 spaces between words)"<< endl;
cin>>morsecode;
cout<< "Morsecode:" << morsecode << endl;
cout << "Text: " << moltoeng(morsecode) << endl;
}
else if ((choice < 1)||(choice > 2))
{
cout << "invalid choice"<<endl;
}
{
cout << " Would you like to continue? Press y to repeat. Press any other key to stop."<<endl;
cin>>repeat;
}
}
return 0;
}
Explanation / Answer
Add this code segment, where you are reading text:
------------------------------------------------
string str;
cin.ignore();
getline(cin,str);
strcpy(text,str.c_str());
------------------------------------------------
This will read whole string, not the one word. Include <cstdio> for these functions to work. Also qualify parameter in strlen() function with const.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.