Comparing two elements of two diff. string arrays for equality

Hi Everyone,
It seems this is an often asked question on c++ forums but I still cant make sense of it. I'm writing a simple conversion program from English to Morse code (plenty of that on the web as well) When trying to compare an element of my English string array with an element of my Morse Array for equality (==) I get an error, no operator matches these operands.

I'm new to coding and just cant see why this isn't working, I cant see where I've made an error. Any help pointing me in the right direction would be greatly appreciated.

Thank you

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
#include<iostream>
#include<string>
using namespace std;

string english[] = { "A", "B", "C", "D", "E", "F", "G", "H", "I", "J", "K", "L", 
					 "L", "M", "N", "O", "P", "Q", "R", "S", "T", "U", "V", "W",
					 "X", "Y", "Z", };
string morse[] = { ".-", "-...", "-.-.", "-..", ".", "..-.", "--.", "....", "..",
				   ".---", "-.-", ".-..", "--", "-.", "---", ".--.", "--.-", ".-.",
				   "...", "-", "..-", "...-", ".--", "-..-", "-.--", "--.." };
string EnglishToMorse;

string MorseToEnglish[];


int main()
{
	cout << "Please enter a word or phrase in English for conversion" << endl;
	getline (cin, EnglishToMorse);
	
	for(int i =1; i <EnglishToMorse.length(); ++i)
	{
		for (int b =1; b < 27; ++b)
		{
			if ( EnglishToMorse[i] == english[b])// the error occurs here
			{
				cout<< morse[b]<< " "<< endl;
	

		}}}
}
EnglishToMorse[i] is a character
english[b] is a string

Other errors are:
"L" twice
lower case letters fail
int i = 1 int b = 1 should start at 0
line 13 not used
once line 28 is hit you can break out of the inner loop
include numbers
Last edited on
Thanks,
I understand what you mean, but I'm not sure what to do about it.
can I change EnglishToMorse to an Array? I'm trying to use cin.getline() to read the input into that array but I haven't got it to work yet.

Would you suggest i make them both strings or both character arrays.

Thank you for help already, I don't mean to Bombard you with questions :)

There are lots of solutions. A simple one is to change line 5 to be a char array. You will have to use single quotes around the letters ('A' not "A").
There doesn't seem to be much point making english an array of strings, as each element is only actually a single character. Unless you need each entry in that array to have the functionality of the string class, then you can just make it an array of chars instead.

Then your comparison between elements of EnglishToMorse and elements of english will be more straightforward.
hi Guys,

I read you said, and had another crack at writing this program, but am still encountering issues, its a logic error somewhere, visual studio gives an error about truncation from int to char. but i have no idea what that actually means. Any help or guidance would be massively appreciated, Thanks

Hers the code:

#include<iostream>
#include<string>
using namespace std;

char english[26] = { '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 morse[26] = { '.-', '-...', '-.-.', '-..', '.', '..-.', '--.', '....', '..',
'.---', '-.-', '.-..', '--', '-.', '---', '.--.', '--.-', '.-.',
'...', '-', '..-', '...-', '.--', '-..-', '-.--', '--..' };
string EnglishToMorse;
string MorseToEnglish;
string MorseWord;
void EnglishToMorseTranslation();
void MorseToEnglishTranslation();
int choice;
int main()
{
cout<<" Please choose from the following menu"<<endl;
cout<<"1) to convert from English to Morse Code"<<endl;
cout<<"2) to convert from Morse Code to English"<<endl;
cin >> choice;

if(choice==1){
EnglishToMorseTranslation( );
}
if (choice==2){
MorseToEnglishTranslation();
}


return 0;
}

void EnglishToMorseTranslation()
{


cout << "Please enter a word or phrase in English for conversion" << endl;
cin >> EnglishToMorse;

for (unsigned i = 0; i < EnglishToMorse.length(); ++i) {
for (unsigned b = 0; b < 26; ++b) {
if ( EnglishToMorse[i] == english[b]) {
cout << morse[b];



}
}
}

}

void MorseToEnglishTranslation()
{
cout << "Please enter a word or phrase in Morse Code for Translation" << endl;
cin >> MorseToEnglish;
for (unsigned i = 0; i < MorseToEnglish.length(); ++i) {
for (unsigned b = 0; b < 26; ++b) {
if ( MorseToEnglish[i] == morse[b]) {

cout<< english[b]<< " "<< endl;

}
}
}

}
Topic archived. No new replies allowed.