Hi, I am new to c++ programming and Im working on a morse code program which has to translate normal text to morse code and vice versa. I have managed to convert text to morse but can't figure out how to convert morse into text, so if anyone could help it would be great. Sorry it's not well commented yet.
//Headerfile som omvandlar normal text till MorseKod.
//Inkluderas i main () som: #include "texttomorse.h"
#include <iostream>
#include <string>
#include "iodos.h"
for (int i=0; textin[i]; i++)
{
switch (toupper(textin[i]))
{
case 'A': out += "._";
break;
case 'B': out += "-...";
break;
case 'C': out += "-.-.";
break;
case 'D': out += "-..";
break;
case 'E': out += ".";
break;
case 'F': out += "..-.";
break;
case 'G': out += "--.";
break;
case 'H': out += "....";
break;
case 'I': out += "..";
break;
case 'J': out += ".---";
break;
case 'K': out += "-.-";
break;
case 'L': out += ".-..";
break;
case 'M': out += "--";
break;
case 'N': out += "-.";
break;
case 'O': out += "---";
break;
case 'P': out += ".--.";
break;
case 'Q': out += "--.-";
break;
case 'R': out += ".-.";
break;
case 'S': out += "...";
break;
case 'T': out += "-";
break;
case 'U': out += "..-";
break;
case 'V': out += "...-";
break;
case 'W': out += ".--";
break;
case 'X': out += "-..-";
break;
case 'Y': out += "-.--";
break;
case 'Z': out += "--..";
break;
case 'Å': out += ".--.-";
break;
case 'Ä': out += ".-.-";
break;
case 'Ö': out += "---.";
break;
default: out;
}
out += " ";
}
return out;
}
//Morse_Kod.cpp
#include <iostream>
#include <string>
#include <iomanip>
#include "iodos.h" //Inkluderings fil, behövs för att läsa in svenska tecken.
#include "texttomorse.h"
using namespace std;
int main ()
{
dos_console(); //deklaration av inkluderings fil iodos.h
cout << "\tA ._ K -.- U ..-\n"
<< "\tB -... L .-.. V ...-\n"
<< "\tC -.-. M -- W .--\n"
<< "\tD -.. N -. X -..-\n"
<< "\tE . O --- Y -.--\n"
<< "\tF ..-. P .--. Z --..\n"
<< "\tG --. Q --.- Å .--.-\n"
<< "\tH .... R .-. Ä .-.-\n"
<< "\tI .. S ... Ö ---.\n"
<< "\tJ .--- T -\n\n\n";
cout << "Välj vad du vill göra\n"
<< "---------------------\n"
<< "(0) Avsluta Program.\n"
<< "(1) Omvandla text till Morse Kod.\n"
<< "(2) Omvandla Morse Kod till Text.\n";
cout << "Val: ";
getline(cin, choice);
if (choice == "1")
{
cout << "\n\nSkriv den text du vill översätta: ";
getline(cin, input);
cout << "\n\n";
cout << "Din text översätt är: " << texttomorse(input);
cout << "\n\n";
}
else if (choice == "2")
{
cout << "\nSkriv den kod du vill översätta :";
getline(cin, input);
cout << texttomorse(input);
cout << "\n\n";
}
else if (choice == "0")
{
cout << "Programmet avslutas, välkommen åter\n\n";
}
else
cout << "\n\aFelaktig inmatning!!!!\n\n";
}while (!(choice == "0"));
}
If there's no spaces or some other form of delimiter between the character sequences of dots and dashes then you can't convert it back from Morse to text. You need to add spaces first off.
Then going back it's just a branching operation in a loop:
switch (first) {
case'.':
switch (next) {
case'.':
switch (next-next) {
case'.':
...
case'-':
...
case' ':
return'I'; //I is "..", so ".. " is an I.
}
case'-':
...
case' ': //space
return'E'; //E is one ".", so ". " is an E.
}
case'-':
switch (next) {
case'.':
...
}
}
I think this is a crappy assignment (if it is one) because you have to type so much out instead of being able to make a better design.
But, I would use a std::map to map the letter to the morse, and another map to map the morse to the letter. That's just my opinion, and you're of course not obligated to use it. Another matter is the problem stravant brought up with lacking delimiters, I think you can solve this yourself, programming is really mainly problem solving anyway, so I wish you the best of luck. Please do at least glance at the std::map template, it's very useful.