Entry tags:
Cross posted to the cpp community
I have to write a program that codes or decodes morse code (depending on which choice the user wants). I'm having some issues with the morse to alphanumeric conversion. The program I wrote reads the code from a file and then spits out the alphanumeric equivalent on the screen. It compiles great, but when I try to run the morse code through it, it prints nothing. So here's my question: why isn't the program reading the data in properly?
This is the code I want to read in:
.- -... -.-.
.---- ..--- ...--
.... . .-.. .-.. ---
.--- . ..-. ..-.
Each individual letter is separated by a space.
I'm also aware that if loops are probably not the most efficient way to get it done, but for right now I'm not really concerned with making it pretty. I just want it to work.
This is the code I want to read in:
.- -... -.-.
.---- ..--- ...--
.... . .-.. .-.. ---
.--- . ..-. ..-.
Each individual letter is separated by a space.
I'm also aware that if loops are probably not the most efficient way to get it done, but for right now I'm not really concerned with making it pretty. I just want it to work.
char morsetoalpha (ifstream &file, char morse[6])
{
while (!file.eof())
{
file >> morse;
if (morse == ".-")
cout << "A";
else if (morse == "-...")
cout << "B";
else if (morse == "-.-.")
cout << "C";
else if (morse == "-..")
cout << "D";
else if (morse == ".")
cout << "E";
else if (morse == "..-.")
cout << "F";
else if (morse == "--.")
cout << "G";
else if (morse == "....")
cout << "H";
else if (morse == "..")
cout << "I";
else if (morse == ".---")
cout << "J";
else if (morse == "-.-")
cout << "K";
else if (morse == ".-..")
cout << "L";
else if (morse == "--")
cout << "M";
else if (morse == "-.")
cout << "N";
else if (morse == "---")
cout << "O";
else if (morse == ".--.")
cout << "P";
else if (morse == "--.-")
cout << "Q";
else if (morse == ".-.")
cout << "R";
else if (morse == "...")
cout << "S";
else if (morse == "-")
cout << "T";
else if (morse == "..-")
cout << "U";
else if (morse == "...-")
cout << "V";
else if (morse == ".--")
cout << "W";
else if (morse == "-..-")
cout << "X";
else if (morse == "-.--")
cout << "Y";
else if (morse == "--..")
cout << "Z";
else if (morse == ".----")
cout << "1";
else if (morse == "..---")
cout << "2";
else if (morse == "...--")
cout << "3";
else if (morse == "....-")
cout << "4";
else if (morse == ".....")
cout << "5";
else if (morse == "-....")
cout << "6";
else if (morse == "--...")
cout << "7";
else if (morse == "---..")
cout << "8";
else if (morse == "----.")
cout << "9";
else if (morse == "-----")
cout << "0";
}
}