PLEASE HELP Text to morse code translation

Hey all.

So I wrote a program to convert ONE letter or number into morse code.
That is, a user can input anything from A-Z, a-z, or 0-9 and have the morse code counterpart returned.

Now I am trying to change the character variable into a character array and do the same conversion, except I want to be able to convert the whole array of letters and numbers into morse code.
That is, I want a user to put in Hello, A123Z, etc. and be able to receive the morse code for it.

I can't get it to work. Also, so far I only have the character array go up to 10 places, but I want it to be unlimited. 10 is just my trial number until I get the project to work.

Here is the source code. What am I doing wrong??


// Morse code conversion

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

int main()
{
// Declare variables
char morse[10];
string converted[10];
char repeat;
int i = 0;

// Create program loop:
do
{

// Prompt user for input:
cout << "Please enter a string of characters or numbers no more than 10 units long:\n";
cin >> morse;

// Perform the conversion:

for (i=0; i<=10; i++)
{

if ((morse[i] == 'A') || (morse[i] == 'a'))
{
converted[i] = " * - ";
}
else if ((morse[i] == 'B') || (morse[i] == 'b'))
{
converted[i] = " - * * * ";
}
else if ((morse[i] == 'C') || (morse[i] == 'c'))
{
converted[i] = " - * - * ";
}
else if ((morse[i] == 'D') || (morse[i] == 'd'))
{
converted[i] = " - * * ";
}
else if ((morse[i] == 'E') || (morse[i] == 'e'))
{
converted[i] = " * ";
}
else if ((morse[i] == 'F') || (morse[i] == 'f'))
{
converted[i] = " * * - * ";
}
else if ((morse[i] == 'G') || (morse[i] == 'g'))
{
converted[i] = " - - * ";
}
else if ((morse[i] == 'H') || (morse[i] == 'h'))
{
converted[i] = " * * * * ";
}
else if ((morse[i] == 'I') || (morse[i] == 'i'))
{
converted[i] = " * * ";
}
else if ((morse[i] == 'J') || (morse[i] == 'j'))
{
converted[i] = " * - - - ";
}
else if ((morse[i] == 'K') || (morse[i] == 'k'))
{
converted[i] = " - * - ";
}
else if ((morse[i] == 'L') || (morse[i] == 'l'))
{
converted[i] = " * - * * ";
}
else if ((morse[i] == 'M') || (morse[i] == 'm'))
{
converted[i] = " - - ";
}
else if ((morse[i] == 'N') || (morse[i] == 'n'))
{
converted[i] = " - * ";
}
else if ((morse[i] == 'O') || (morse[i] == 'o'))
{
converted[i] = " - - - ";
}
else if ((morse[i] == 'P') || (morse[i] == 'p'))
{
converted[i] = " * - - * ";
}
else if ((morse[i] == 'Q') || (morse[i] == 'q'))
{
converted[i] = " - - * - ";
}
else if ((morse[i] == 'R') || (morse[i] == 'r'))
{
converted[i] = " * - * ";
}
else if ((morse[i] == 'S') || (morse[i] == 's'))
{
converted[i] = " * * * ";
}
else if ((morse[i] == 'T') || (morse[i] == 't'))
{
converted[i] = " - ";
}
else if ((morse[i] == 'U') || (morse[i] == 'u'))
{
converted[i] = " * * - ";
}
else if ((morse[i] == 'V') || (morse[i] == 'v'))
{
converted[i] = " * * * - ";
}
else if ((morse[i] == 'W') || (morse[i] == 'w'))
{
converted[i] = " * - - ";
}
else if ((morse[i] == 'X') || (morse[i] == 'x'))
{
converted[i] = " - * * - ";
}
else if ((morse[i] == 'Y') || (morse[i] == 'y'))
{
converted[i] = " - * - - ";
}
else if ((morse[i] == 'Z') || (morse[i] == 'z'))
{
converted[i] = " - - * * ";
}
else if (morse[i] == '1')
{
converted[i] = " * - - - - ";
}
else if (morse[i] == '2')
{
converted[i] = " * * - - - ";
}
else if (morse[i] == '3')
{
converted[i] = " * * * - - ";
}
else if (morse[i] == '4')
{
converted[i] = " * * * * - ";
}
else if (morse[i] == '5')
{
converted[i] = " * * * * * ";
}
else if (morse[i] == '6')
{
converted[i] = " - * * * * ";
}
else if (morse[i] == '7')
{
converted[i] = " - - * * * ";
}
else if (morse[i] == '8')
{
converted[i] = " - - - * * ";
}
else if (morse[i] == '9')
{
converted[i] = " - - - - * ";
}
else if (morse[i] == '0')
{
converted[i] = " - - - - - ";
}
else
{
cout << "That is an invalid character.";
}

i += 1;
}


// Print out results:
cout << "\n\n" << morse << " is equal to " << converted << " in morse code.\n\n\n";
cout << "Would you like to convert anything else, [Y]es or [N]o? ";
cin >> repeat;
cout << "\n\n\n";
} while ((repeat == 'Y') || (repeat == 'y'));


return 0;
}
Last edited on
consider using <string>.
How?
Take this as an example

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
32
33
#include <iostream>
#include <string>
using namespace std;

int main()
{

    string UserSentence;

    getline(cin, UserSentence); //This allows as many characters as the user wants, and spaces too.

    string ConvertedToMorse [ UserSentence.length() ]; //Creating the array of strings using the amount of characters of the user sentence

    for(int i = 0; i < UserSentence.length(); i += 1)
    {

        if(UserSentence.at(i)=='A' || UserSentence.at(i)=='a')
            ConvertedToMorse[i]="* -";

        else if (UserSentence.at(i)=='B' || UserSentence.at(i)=='b')
            ConvertedToMorse[i]="- * * *";

        // else if (/*And so on*/)

        else ConvertedToMorse[i]=" "; //Else, only put a space
    }

    for (int x = 0; x < UserSentence.length(); x += 1)
        cout << ConvertedToMorse[x] << "  ";

    cout << endl;
    cin.get();
}

Oh, and you increased the value of 'i' two times, notice i += 1 at the end of your for loop
Last edited on
Ok, I fixed the program but I am still getting an error message (Actually 3).

It is not letting me set the string array length.

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

// Morse code conversion

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

int main()
{

	// Declare variables
	string morse;
	char repeat;
	int i = 0;
	int x;


	cout << "Please enter a string of characters or numbers:\n";
	getline(cin, morse);
	
	x = morse.length();
	string converted[x];


return 0;
}


Suggestions?
Topic archived. No new replies allowed.