Morse Code Project Help

If we are not allowed to use arrays in this project, I am confused on how to make "a morse code get/printing function that converts an individual char into a morse code string". Thank you.


Instructions link:

https://docs.google.com/document/d/1QADrQqbhcfTSin28ArZ03t7Sio4D49eP2cJnchCgsyM/edit
By that, I'm not sure if your instructor is forbidding using a string like this or not:

1
2
std::string name = "Hello World!";
name[1];


A string is really just an array and so using "[]" to navigate the string is a good way of doing so. He says not to use "Array Code", which is pretty vague and a weird way to describe what he's trying to say in any case.

However, you can still use a string in pretty much the exact same way without that operator like this:

1
2
std::string name = "Hello World!";
std::cout << name.at(0);


And so you can loop as you would need to for your project like this:

1
2
3
4
std::string name = "Hello World!";

for (int i = 0; i < name.size(); i++)
	std::cout << name.at(i);



EDIT: Your other post was reported by someone, you shouldn't double post on a different section of the forum. It's the same members.
Last edited on
> You will have several functions and lots of IF/ELSE statements. DO NOT USE ARRAY CODE
I understand that you may have
1
2
3
4
5
6
7
8
for c in string:
   if is_alpha(c):
      morse_letter(c)
   elif is_digit(c):
      morse_digit(c)
   elif is_symbol(c):
      morse_symbol(c)
   //... 
but if the idea of the «avoid array» part is that your morse_letter() function has an if for each
letter in A-Z, then your teacher is an imbecile and will only make the students to hate the class.

using a dictionary
1
2
3
4
5
6
std::map<char, std::string> morse_code;
//fill from a file
char character;
std::string code;
while(fin >> character >> code)
   morse_code[character] = code;
then may retrieve the code by doing morse_code[character] (if the character were not present in the dictionary, it will add it with an empty string)
Last edited on
Please don't post duplicates.
http://www.cplusplus.com/forum/general/264100/
DO NOT USE ARRAY CODE.
Smack my head. He may as well have said "you MUST write bad code for this project."

Okay, to avoid array code, you can use a switch() statement to convert from a character to morse code. The great irony here is that the compiler will probably turn this into an array of sorts (a jump table).
1
2
3
4
5
6
7
8
9
10
11
const char *toMorse(char ch)
{
    switch (ch) {
    case 'a': return ".-";
    case 'b': return "-...";
    ...
    case '0': return "-----";
    case '1': return ".----";
    // etc for all the letters, digits and punctuation
    default: return "#"
}


Ask the professor if it's okay to use array syntax to go through the input strings. If not then you might use pointers instead.

I'd divide this up so that one person is writing the user interface and the other person is writing the toMorse() function and the function to print a sentence.

To do it as a pair, create a header file that declares those two functions and contains comments explaining exactly what they do.

Then each of you goes off and writes his/her part. The one writing toMorse and printSentence (or whatever you call it) will create their own main() program to test these functions. Meanwhile, the other partner, the one writing the "real" main() function and user interface, will create dummy versions of toMorse() and printSentence() so that he/she can test the user interface. The dummy toMorse() might just return "blip" and printSentence() might print "this is a sentence".

If you do this right, then when you're both done with your part, you'll just have to get together, combine the two parts, delete the test code that each of you have, and voila! The program should work.
I posted the code for all the strings in the array as a tree thread if you don't want to re-type it.
it uses arrays but the letter to string bit can be copied to save time.
I've often wondered what goes through the mind of these "professors" who dream up these "do this without using the obvious" assignments.

It's like the old "swap two variables without using a temporary". It's a head scratcher if you don't know the answer, and it's a stupid magic trick you'll never use in an actual program if you do know it.

That's all these kinds of exercises are - stupid magic tricks.

Most of the long time programmers here can figure out all sorts of silly answers to these kinds of problems that leave all the noobs in a complete fog.

But none of this is actually teaching the noobs anything useful like actually how to write clear understandable programs - you know, ones that will earn you a paycheck.

Professors who make these assignments don't know shit, and it's merely an exercise which allows them to feel like smug gits while the students suffer.
use dhayden's helper function

have a void function for the menu
in main call

menu();
cin >> a_string;

use a for loop to traverse through the string and use the helper function provided
to convert the letters to morse

finished
if you plan to use a switch
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
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
case 'A': return  ".-";
case 'B': return  "-...";
case 'C': return  "-.-.";
case 'D': return  "-..";
case 'E': return  ".";
case 'F': return  "..-.";
case 'G': return  "--.";
case 'H': return  "....";
case 'I': return  "..";
case 'J': return  ".---";
case 'K': return  "-.-";
case 'L': return  ".-..";
case 'M': return  "--";
case 'N': return  "-.";
case 'O': return  "---";
case 'P': return  ".--.";
case 'Q': return  "--.-";
case 'R': return  ".-.";
case 'S': return  "...";
case 'T': return  "-";
case 'U': return  "..-";
case 'V': return  "...-";
case 'W': return  ".--";
case 'X': return  "-..-";
case 'Y': return  "-.--";
case 'Z': return  "--..";

case '0': return  "-----";
case '1': return  ".----";
case '2': return  "..---";
case '3': return  "...--";
case '4': return  "....-";
case '5': return  ".....";
case '6': return  "-....";
case '7': return  "--...";
case '8': return  "---..";
case '9': return  "----.";

case '.': return  ".-.-.-";
case ',': return  "--..--";
case ':': return  "---...";
case '?': return  "..--..";
case '\'': return  ".----.";
case '-': return  "-....-";
case '/': return  "-..-. ";
case '\"': return  ".-..-.";
case '@': return  ".--.-.";

case ' ': return "/";
default: return "#";
note that I've used uppercase, ask if you want them lowercase
still, your code should convert each character to lowercase or uppercase before converting

ask if you want it with if-else instead of switch
A slightly obscure way is to define your whole morse conversion dictionary as a string; e.g. with your morse code in brackets following its character:

const string dictionary = 
    string( "A[.-]B[-...]C[-.-.]D[-..]E[.]F[..-.]G[--.]H[....]I[..]J[.---]K[-.-]L[.-..]M[--]"         )
  + string( "N[-.]O[---]P[.--.]Q[--.-]R[.-.]S[...]T[-]U[..-]V[...-]W[.--]X[-..-]Y[-.--]Z[--..]"       )
  + string( "0[-----]1[.----]2[..---]3[...--]4[....-]5[.....]6[-....]7[--...]8[---..]9[----.]"        )
  + string( R"(.[.-.-.-],[--..--]:[---...]?[..--..]'[.----.]-[-....-]/[-..-.]"[.-..-.]@[.--.-.] [/])" );


Then you can use std::string.find() with target c+"[", followed by target "]". Then just return a std::string.substr().

It's only a few lines of code.
Last edited on
Topic archived. No new replies allowed.