This is for an assignment and I can't use c++ strings... Only c-style strings. Any help on solving this problem would be great!
Errors I'm getting:
a5.cpp:45:10: error: invalid operands to binary expression ('char [1]' and 'int')
output += ' ';
~~~~~~ ^ ~~~
a5.cpp:59:27: error: excess elements in char array initializer
char morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.-...
^~~~~~
a5.cpp:75:16: error: invalid operands to binary expression ('char [255]' and 'int')
output += alpha_num[position];
~~~~~~ ^ ~~~~~~~~~~~~~~~~~~~
a5.cpp:82:10: error: invalid operands to binary expression ('char [255]' and 'int')
output += ' ';
~~~~~~ ^ ~~~
#include <cstdlib>
#include <istream>
using namespace std;
void engtomol(char *text);
char engtomol(char *text){
char *morse_code[]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};
char alpha_num[]={'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 message[255];
char output[]="";
size_t index = 0;
size_t const size = sizeof(message);
while (index < size){
int position = 0;
if (message[index] != ' '){
while (alpha_num[position] != message[index] && position < 36){
++position;
}
output += morse_code[position];
}
else{
output += ' ';
}
}
return 0;
}
char moltoeng(char* morsecode){
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[255];
size_t index = 0;
size_t const size = sizeof(morsecode);
while(index < size){
string letter = "";
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++;
}
if(count >= 7){
output += ' ';
}
}
return 0;
}
int main()
{
char *text;
char *morsecode;
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.get();
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.get();
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.";
cin>>repeat;
}
}
return 0;
}
Last edited on