Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

This is for an assignment and I can\'t use c++ strings... Only c-style strings.

ID: 3672962 • Letter: T

Question

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

#include

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)?"<

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) <

}

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"<

}

{

cout << " Would you like to continue? Press y to repeat. Press any other key to stop.";

cin>>repeat;

}

}

return 0;

}

Explanation / Answer

Lines that were causing errors are commented and mentioned in bold

#include <cstdlib>
#include <istream>
#include <cstring>
#include<iostream> //include iostream to use cout and cin
using namespace std;

//void engtomol(char *text); line commented

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];
char *c=morse_code[position];
strncat(output, c, 1);
}
else{
//output += ' ';
char c=' ';
strncat(output, &c, 1);
}
}
return 0;
}
char moltoeng(char* morsecode){
//below line commented
//char morse_code[36]={".-","-...","-.-.","-..",".","..-.","--.","....","..",".---","-.-",".-..","--","-.","---",".--.","--.-",".-.","...","-","..-","...-",".--","-..-","-.--","--..","-----",".----","..---","...--","....-",".....","-....","--...","---..","----."};

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]; line commented
char c=alpha_num[position];
strncat(output,&c,1);
int count = 0;
while(morsecode[index] == ' '&& index < size){
index++;
count++;
}
if(count >= 7){
//output += ' '; line commented
char c=' ';
strncat(output,&c,1);
}
}
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(); line commented
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.get(); line commented
cin>>morsecode;
cout<< "Morsecode:" << morsecode << endl;
//cout << "Text: " << moltoeng(&morsecode) << endl;line commented
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;
}

Hire Me For All Your Tutoring Needs
Integrity-first tutoring: clear explanations, guidance, and feedback.
Drop an Email at
drjack9650@gmail.com
Chat Now And Get Quote